I've migrated my internationalization module to use Fluent and FTL instead of simple JSON resources. It allows you to load FTLs with a very simple API. It also uses ICU4X.
This is... rialight::intl! I'll also implement Ecma-like Intl things, like DisplayNames
and Collator
.
Now using FTLs it looks like this:
use rialight::intl::ftl::{Ftl, FtlOptions, FtlOptionsForAssets, FtlLoadMethod};
use rialight::util::{hashmap};
let app_ftl = Ftl::new(
FtlOptions::new()
// specify supported locales.
// the form in which the locale identifier appears here
// is a post-component for the assets "src" path.
// for example: "path/to/res/lang/en-US"
.supported_locales(vec!["en"])
.default_locale("en")
.fallbacks(hashmap! {
"pt" => vec!["en"],
})
.assets(FtlOptionsForAssets::new()
// for file system load method you can also use the app: scheme.
// the "app:" scheme works for Rialight applicaitons only.
.source("res/lang")
.files(vec!["_"])
// "clean_unused" indicates whether to clean previous unused locale data.
.clean_unused(true)
// specify FtlLoadMethod::FileSystem or FtlLoadMethod::Http
.load_method(FtlLoadMethod::FileSystem))
); // app_ftl
if !app_ftl.load(None).await {
// failed to load
return;
}
println!("{}", app_ftl.get_message("hello-world", None, &mut vec![]).unwrap());
Our res/lang/en/_.ftl:
hello-world = Hello, world!