Incular logoIncular
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

TypePurpose
LocaleBCP-47 tag with parent fallback (fr-CA → fr)
LocaleResolverResolve requested locales against supported ones
LocalizationCatalogApp-owned message source trait
LocalizationErrorLookup/catalog failure
LocalizedMessageResolved message with locale
PluralCategoryzero / one / two / few / many / other
PluralFormsPer-category message forms

RuntimeEnvironment::resolve_locale(&supported) and BuildContext::resolve_locale pick the best match; arabic locales flip Directionality to Rtl.

Related: Directionality, Restoration.

On this page