Fluent4s is a Scala implementation of The Project Fluent, a new spec for translation files.
- Functional error handling
- Error accumulation
- Fluent Spec
SBT
libraryDependencies += "io.github.fluent4s" %% "fluent4s-core" % "version"
libraryDependencies += "io.github.fluent4s" %% "fluent4s-parser" % "version"
Mill
ivy"io.github.fluent4s::fluent-core:version"
ivy"io.github.fluent4s::fluent-parser:version"
The user api of Fluent4s is located inside the io.github.fluent4s.api
package:
import io.github.iltotore.fluent4s.api._
Also import the parser of your choice. Here is the default parser:
import io.github.iltotore.fluent4s.parser.Ftl.Parser
You firstly need to use the decode
method to parse and resolve your fluent text:
val ftl =
"""# Simple things are simple.
|hello-user = Hello, {$userName}!
|
|# Complex things are possible.
|shared-photos =
| {$userName} {$photoCount ->
| [one] added a new photo
| *[other] added {$photoCount} new photos
| } to {$userGender ->
| [male] his stream
| [female] her stream
| *[other] their stream
| }.""".stripMargin
val resource: FluentResource = decode(ftl, Locale.ENGLISH)
FluentResource has two methods to get a message from a key:
val resource: FluentResource = ???
//Using tuples
resource.getMessage("shared-photos")(
"userName" -> FluentValue.Text("Il_totore"),
"photoCount" -> FluentValue.Number(5),
"userGender" -> "male"
)
//Using a Map
val args = Map(
"userName" -> FluentValue.Text("Il_totore"),
"photoCount" -> FluentValue.Number(5),
"userGender" -> "male"
)
resource.getMessageWith("shared-photos")(args)
Find more information about methods and classes in the Scaladoc