API ReferenceEnvironment
Localization
Locales, catalogs, messages, and plural rules with ICU fallback.
Localizations { locale: Locale, catalog, child: Widget } scopes a typed
locale plus an app-owned catalog.
use incular::prelude::*;
struct Catalog {
locales: Vec<Locale>,
}
impl LocalizationCatalog for Catalog {
fn supported_locales(&self) -> &[Locale] {
&self.locales
}
fn message(&self, locale: &Locale, key: &str) -> Option<&str> {
(locale.to_string() == "en" && key == "greeting").then_some("Hello")
}
}
fn main() {
let locale: Locale = "en".parse().expect("valid locale");
let catalog = Catalog {
locales: vec!["en".parse().expect("valid locale")],
};
let scope = Localizations::new(locale, catalog, Text::new("Hi"))
.with_locale("en".parse().expect("valid locale"));
println!("locale: {}", scope.locale());
println!("text: {:?}", scope.text("greeting"));
println!("dir: {:?}", scope.text_direction());
let app = Application::new(|_cx| {
Localizations::new(
"en".parse().expect("valid locale"),
Catalog {
locales: vec!["en".parse().expect("valid locale")],
},
Text::new("Hi"),
).into()
})
.expect("valid application");
incular::run(app).expect("run application");
}Types
| Type | Purpose |
|---|---|
Locale | BCP-47 tag with parent fallback (fr-CA → fr) |
LocaleResolver | Resolve requested locales against supported ones |
LocalizationCatalog | App-owned message source trait |
LocalizationError | Lookup/catalog failure |
LocalizedMessage | Resolved message with locale |
PluralCategory | zero / one / two / few / many / other |
PluralForms | Per-category message forms |
RuntimeEnvironment::resolve_locale(&supported) and
BuildContext::resolve_locale pick the best match; arabic locales flip
Directionality to Rtl.
Related: Directionality,
Restoration.