Skip to content

Using fluent4j in a Java project

QuickWrite edited this page Sep 26, 2022 · 3 revisions

Installation

fluent4j can easily be included in an existing Maven or Gradle project by just adding it as a dependency:

Maven

<repositories>
    ...
    <repository>
        <id>quickwrite-net-fluent4j</id>
        <url>https://dl.cloudsmith.io/public/quickwrite-net/fluent4j/maven/</url>
    </repository>
    ...
</repositories>
<dependencies>
    ...
    <dependency>
        <groupId>quickwrite-net</groupId>
        <artifactId>fluent4j</artifactId>
        <version>{{package-version}}</version>
    </dependency>
    ...
</dependencies>

Gradle

repositories {
    ...
    maven {
        url "https://dl.cloudsmith.io/public/quickwrite-net/fluent4j/maven/"
    }
    ...
}
dependencies {
    ...
    implementation 'net.quickwrite:fluent4j:{{package-version}}'
    ...
}

Using it

Fluent is based on different Fluent Resources. These Resources do contain the content and need to be parsed by the parser and then added into a bundle. In these bundles the messages can be accessed with the different locales and functions so that the different messages can be changed depending on the region.

FluentResource resource = FluentParser.parse("hello = World");
FluentBundle bundle = new ResourceFluentBundle(ULocale.ENGLISH, resource);

To access the messages that were saved inside the FluentBundle the getMessage method should be used. If the getMessage method does get null as the arguments parameter it automatically defaults on using the FluentArgs.EMPTY_ARGS as the parameter. It is returning an optional if the message exists:

//                                                                    The key ↓       ↓ The arguments
System.out.println("The message for the key 'hello': " + bundle.getMessage("hello", null).get());

This would print out:

The message for the key 'hello': World

Using builders

To make the process a little bit easier the builder pattern is used to make the creation of FluentBundles easier:

FluentBundle bundle = new FluentBundleBuilder(ULocale.ENGLISH, "hello = World").build();

If more resources need to be added the addResource and if functions should be added the addFunction method can be used.

Clone this wiki locally