Skip to content

Commit 5829fb2

Browse files
authored
Merge pull request #29 from QuickWrite/develop
Version 1.0.0
2 parents 73ef170 + 92873f4 commit 5829fb2

245 files changed

Lines changed: 6782 additions & 6344 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,53 @@ dependencies {
4343
}
4444
```
4545

46-
## Usage
46+
## What is `fluent4j`?
47+
The `fluent4j` library is a Java implementation of [Mozillas Fluent project](https://www.projectfluent.org/) that
48+
intends to be extensible by default.
4749

48-
You could either use the library directly without all of the abstraction and create all of the
49-
objects by yourself:
50+
This means that custom constructs can be added to the basic fluent syntax so that the translation files can be used
51+
for the projects exact needs.
5052

53+
## Usage
54+
So that the translation files can be used the files need to be parsed first:
5155
```java
52-
FluentResource resource = FluentParser.parse("emails = You have { $unreadEmails } unread emails.");
53-
FluentBundle bundle = new ResourceFluentBundle(ULocale.ENGLISH, resource);
56+
ResourceParser resourceParser = ResourceParserBuilder.defaultParser();
5457

55-
FluentArgs arguments = new FluentArguments();
56-
arguments.setNamed("unreadEmails", new NumberLiteral(10));
58+
FluentResource resource = resourceParser.parse(FluentIteratorFactory.fromString("""
59+
test = This is your fluent file
5760
58-
System.out.println(bundle.getMessage("emails", arguments).get());
61+
emails = You have { $unreadEmails } unread emails.
62+
"""));
5963
```
6064

61-
or you could use the builders that the library provides for this:
65+
After you've created a single or multiple Resources you can bundle them in a Bundle with other data for use:
66+
```java
67+
FluentBundle bundle = FluentBundleBuilder.builder(Locale.ENGLISH)
68+
.addResource(resource)
69+
.build();
70+
```
6271

72+
And now you can use the different messages for translation:
6373
```java
64-
FluentBundle bundle = new FluentBundleBuilder(ULocale.ENGLISH, "emails = You have { $unreadEmails } unread emails.")
65-
.build();
74+
System.out.println(bundle.resolveMessage("test", new StringResultBuilder()).get());
75+
```
6676

67-
FluentArgs arguments = new FluentArgsBuilder().set("unreadEmails", 10).build();
77+
```console
78+
This is your fluent file
79+
```
80+
81+
And you can also provide arguments for the messages:
82+
```java
83+
FluentArguments arguments = ArgumentListBuilder.builder()
84+
.add("unreadEmails", 5)
85+
.build();
6886

69-
System.out.println(bundle.getMessage("emails", arguments).get());
87+
System.out.println(bundle.resolveMessage("emails", arguments, new StringResultBuilder()).get());
7088
```
7189

72-
In both cases they would print the message `You have 10 unread emails.`.
90+
```console
91+
You have 5 unread emails.
92+
```
7393

7494
## License
7595
This project is licensed under the permissive [Apache 2.0 license](LICENSE).

api/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>net.quickwrite</groupId>
8+
<artifactId>fluent4j</artifactId>
9+
<version>0.2.3-alpha</version>
10+
</parent>
11+
12+
<artifactId>api</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>16</maven.compiler.source>
16+
<maven.compiler.target>16</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.quickwrite.fluent4j.ast;
2+
3+
import net.quickwrite.fluent4j.ast.pattern.ArgumentList;
4+
import net.quickwrite.fluent4j.ast.placeable.FluentPlaceable;
5+
import net.quickwrite.fluent4j.container.FluentScope;
6+
7+
/**
8+
* The FluentFunction interface represents a function.
9+
* Functions provide additional functionality available to the
10+
* localizers for formatting data or providing additional data.
11+
*/
12+
public interface FluentFunction {
13+
/**
14+
* Retrieves the identifier of the function.
15+
*
16+
* @return The identifier of the function
17+
*/
18+
String getIdentifier();
19+
20+
/**
21+
* Evaluates the function with the given scope and argument list.
22+
*
23+
* @param scope The FluentScope used for evaluating
24+
* @param argumentList The ArgumentList containing the arguments passed to the function
25+
* @return A FluentPlaceable representing the evaluated function
26+
*/
27+
FluentPlaceable parseFunction(final FluentScope scope, final ArgumentList argumentList);
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.quickwrite.fluent4j.ast;
2+
3+
import net.quickwrite.fluent4j.container.FluentScope;
4+
import net.quickwrite.fluent4j.exception.FluentPatternException;
5+
6+
/**
7+
* A pattern itself is the base element for the
8+
* message. With that a pattern can be a TextElement
9+
* or a {@link net.quickwrite.fluent4j.ast.placeable.FluentPlaceable}.
10+
*/
11+
public interface FluentPattern extends FluentResolvable {
12+
/**
13+
* Returns the element that this element links to. If the element
14+
* itself for example is a Message Reference the message itself should be returned.
15+
*
16+
* <hr />
17+
*
18+
* If the element contains the necessary information directly
19+
* (like a simple Text Element) this should return itself.
20+
*
21+
* @param scope The scope that the link should be unwrapped in
22+
* @return The linked element
23+
*
24+
* @throws FluentPatternException If the linked element does not exist this Exception should be thrown
25+
*/
26+
FluentPattern unwrap(final FluentScope scope) throws FluentPatternException;
27+
28+
/**
29+
* Returns a simple string variant of the data that this element contains
30+
*
31+
* @param scope The scope in which this operation should be done
32+
* @return A simple string
33+
*/
34+
String toSimpleString(final FluentScope scope);
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.quickwrite.fluent4j.ast;
2+
3+
import net.quickwrite.fluent4j.container.FluentScope;
4+
import net.quickwrite.fluent4j.result.ResultBuilder;
5+
6+
/**
7+
* The base element for the construction of the message.
8+
*/
9+
public interface FluentResolvable {
10+
/**
11+
* @param scope The scope which contains the necessary information for this element.
12+
* @param builder The ResultBuilder used for resolving the entity.
13+
*/
14+
void resolve(final FluentScope scope, final ResultBuilder builder);
15+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package net.quickwrite.fluent4j.ast.entry;
2+
3+
import net.quickwrite.fluent4j.ast.FluentResolvable;
4+
import net.quickwrite.fluent4j.ast.identifier.FluentIdentifier;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
public interface FluentAttributeEntry extends FluentEntry {
10+
/**
11+
* Returns a list of attributes that this entry has.
12+
*
13+
* <p>
14+
* If there are no entries this should return
15+
* an empty list.
16+
* </p>
17+
*
18+
* @return A list of attributes
19+
*/
20+
FluentAttributeEntry.Attribute[] getAttributes();
21+
22+
/**
23+
* Returns the specific attribute with the given
24+
* identifier.
25+
* <br />
26+
* If the attribute doesn't exist it will return
27+
* an empty optional.
28+
*
29+
* <p>
30+
* This search for the attribute is a linear search
31+
* with the default implementation and with that has
32+
* a time complexity of {@code O(n)}.
33+
* </p>
34+
*
35+
* @param identifier The identifier of the attribute
36+
* @return The attribute
37+
*/
38+
default Optional<FluentAttributeEntry.Attribute> getAttribute(final String identifier) {
39+
for (final FluentAttributeEntry.Attribute attribute : getAttributes()) {
40+
if(attribute.getIdentifier().getSimpleIdentifier().equals(identifier)) {
41+
return Optional.of(attribute);
42+
}
43+
}
44+
45+
return Optional.empty();
46+
}
47+
48+
/**
49+
* A single attribute of the FluentEntry.
50+
*/
51+
interface Attribute extends FluentResolvable {
52+
/**
53+
* Returns the identifier of the attribute
54+
*
55+
* @return The identifier
56+
*/
57+
FluentIdentifier<String> getIdentifier();
58+
59+
/**
60+
* Returns a boolean if the attribute itself can
61+
* be successfully used in a selectable.
62+
*
63+
* @return If the attribute is selectable
64+
*/
65+
boolean isSelectable();
66+
}
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.quickwrite.fluent4j.ast.entry;
2+
3+
import net.quickwrite.fluent4j.ast.FluentPattern;
4+
import net.quickwrite.fluent4j.ast.FluentResolvable;
5+
import net.quickwrite.fluent4j.ast.identifier.FluentIdentifier;
6+
7+
/**
8+
* Represents a single entry that is being used
9+
* inside the parsed resource that holds the translation
10+
* content based upon an identifier.
11+
*/
12+
public interface FluentEntry extends FluentResolvable, FluentPattern {
13+
/**
14+
* Returns the identifier of this specific entry
15+
*
16+
* @return The identifier
17+
*/
18+
FluentIdentifier<String> getIdentifier();
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.quickwrite.fluent4j.ast.entry;
2+
3+
/**
4+
* Represents a FluentMessage specifically
5+
*/
6+
public interface FluentMessage extends FluentAttributeEntry {
7+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.quickwrite.fluent4j.ast.identifier;
2+
3+
/**
4+
* The FluentIdentifier interface represents an identifier.
5+
* Identifiers are used to represent names or keys within the localization messages.
6+
*
7+
* @param <I> The type of the identifier
8+
*/
9+
public interface FluentIdentifier<I> {
10+
/**
11+
* Retrieves the simple form of the identifier.
12+
* The simple form typically represents the short name or key.
13+
*
14+
* @return The simple form of the identifier
15+
*/
16+
I getSimpleIdentifier();
17+
18+
/**
19+
* Retrieves the full form of the identifier.
20+
* The full form may represent the complete identifier with any additional context or namespace.
21+
*
22+
* @return The full form of the identifier
23+
*/
24+
I getFullIdentifier();
25+
26+
/**
27+
* Calculates the hash code of the identifier.
28+
*
29+
* @return The hash code of the identifier
30+
*/
31+
int hashCode();
32+
33+
/**
34+
* Compares this identifier to the specified object for equality.
35+
* Two identifiers are considered equal if their simple forms are equal.
36+
*
37+
* @param o The object to compare with
38+
* @return true if the specified object is equal to this identifier, false otherwise
39+
*/
40+
boolean equals(final Object o);
41+
}

0 commit comments

Comments
 (0)