-
Notifications
You must be signed in to change notification settings - Fork 0
Using fluent4j in a Java project
fluent4j
can easily be included in an existing Maven or Gradle project by just adding it as a dependency:
<repositories>
<repository>
<id>quickwrite-net-fluent4j</id>
<url>https://dl.cloudsmith.io/public/quickwrite-net/fluent4j/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.quickwrite</groupId>
<artifactId>fluent-builder</artifactId>
<version>{{package-version}}</version>
</dependency>
</dependencies>
repositories {
maven {
url "https://dl.cloudsmith.io/public/quickwrite-net/fluent4j/maven/"
}
}
dependencies {
implementation 'net.quickwrite:fluent-builder:{{package-version}}'
}
Fluent is based on different Fluent Resources. These Resources themselves are the parsed version of Fluent files or other types of content (like this string here). They are an intermediate representation for the system which can be combined into a Bundle.
FluentResource resource = ResourceParserBuilder.defaultParser().parse(FluentIteratorFactory.fromString("hello = World"));
To be able to use these Resources they must be added towards a FluentBundle
. This Bundle contains the different messages in the FluentResource
s, but also the locale (can be interpreted as the language) it should use and different functions that the messages can access. This FluentBundle
is being built using the FluentBundleBuilder
class:
FluentBundle bundle = FluentBundleBuilder.builder(Locale.ENGLISH)
.addResource(resource)
.build();
In this case we build a FluentBundle
which contains a single FluentResource
, has the default english locale and does not contain any functions.
To access and resolve the message with the context the .resolveMessage
-Method is being used which accepts the key of the message (in this case it can only be hello
) and an instance of a ResultBuilder
which it also returns in an Optional
(It can also accept arguments in an overloaded version). The ResultBuilder
is being used to concatenate all of the different parts of the message.
// The key ↓ ↓ The ResultBuilder
System.out.println(bundle.resolveMessage("hello", StringResultFactory.construct()).get());
In this case the .resolveMessage
-method returns a valid Optional
with the ResultBuilder
that has been constructed which can be accessed through the .get
-method of the Optional
.
If the program now gets run the console output would be:
World