Fluent-Typed
When using translation keys, there is often no easy way to know if they are being used
correctly and if they are being used at all. This project generates, using the fluent
ast,
the function definitions for the translation keys in a fluent file.
In order to guarantee the safeness, funtions are only generated for messages that are found in all the locales. For those only found for some locales or if the signature of the messages are different a warning is printed.
Each locale's ftl resources are appended into a single ftl file, and you can configure it to either embed all of them into the binary with accessors suitable both for server-side where all of them loaded at startup and accessed via a LazyLock, or client-side where a single one is loaded and then can be used in a signal. This single ftl file can be compressed to your liking using a hook.
You also have the freedom to handle the loading of them yourself, which is especially useful if you want to download a single language at a time without the need for storing them in the binary.
A little extra feature is that if you name one of the messages as language-name
and it
doesn't use any variables plus it's present in all languages, then the generated L10n
enum will also contain the names of all the languages, which is really useful when you
want to present the user with a drop-down menu listing all the available languages.
Usage
# in Cargo.toml
[]
= 0.1
[]
= { = "0.1", = ["build"] }
use ;
// in build.rs
// in lib.rs or main.rs
use L10n;
Type deduction
Since the fluent syntax doesn't explicitly specify the type of the translation variables, this project uses the following rules to infer the type of the translation variables:
- String:
- If a variable's comment contains
(String)
, as in# $name (String) - The name.
- If a variable's comment contains
- Number:
- If a variable's comment contains
(Number)
, as in# $count (Number) - How many.
- If a NUMBER function is used, asin
dpi-ratio = Your DPI ratio is { NUMBER($ratio) }
- If a selector only contains numbers
and CLDR plural catagories:
zero
,one
,two
,few
,many
, andother
. Example:your-rank = { NUMBER($pos, type: "ordinal") -> [1] You finished first! [one] You finished {$pos}st [two] You finished {$pos}nd [few] You finished {$pos}rd *[other] You finished {$pos}th }
- If a variable's comment contains