rialight::localization
LocaleBundle
To use LocaleBundle
, add these dependencies to Cargo.toml
:
toml
[dependencies]
rialight_localization = "1"
maplit = "1.0"
tokio = { version = "1", features = ["full"] }
Example asset located at res/lang/en/_.json
:
{
"message_id": "Some message",
"parameterized": "Here: $x",
"contextual_male": "Male message",
"contextual_female": "Female message",
"contextual_other": "Other message",
"qty_empty": "Empty ($number)",
"qty_one": "One ($number)",
"qty_multiple": "Multiple ($number)"
}
Example usage:
``` use rialightlocalization::{ LocaleBundle, LocaleBundleOptions, LocaleBundleOptionsForAssets, LocaleBundleLoadMethod, bundlevars, }; use maplit::hashmap;
async fn main() { let mut bundle = LocaleBundle::new( LocaleBundleOptions::new() // Specify supported locale codes. // The form in which the locale code appears here // is a post-component for the assets "src" path. // For example: "path/to/res/lang/en-US" .supportedlocales(vec!["en", "pt-BR"]) .defaultlocale("en-US") .fallbacks(hashmap! { "pt-BR" => vec!["en-US"], }) .assets(LocaleBundleOptionsForAssets::new() .src("res/lang") .basefilenames(vec![""]) // "cleanunused" indicates whether to clean previous unused locale data. .cleanunused(true) // Specify LocaleBundleLoadMethod::FileSystem or LocaleBundleLoadMethod::Http .loadmethod(LocaleBundleLoadMethod::FileSystem)) ); // bundle
if !bundle.load(None).await {
// failed to load
return;
}
println!("{}", bundle.get("_.message_id"));
println!("{}", bundle.get_formatted("_.parameterized", vec![ &bundle_vars!{
"x" => "foo"
} ]));
println!("{}", bundle.get_formatted("_.contextual", vec![ &"female" ]));
} ```