Skip to main content

use_init_i18n

Function use_init_i18n 

pub fn use_init_i18n(init: impl FnOnce() -> I18nConfig) -> I18n
Available on crate feature i18n only.
Expand description

Initialize an I18n instance and provide it to descendant components.

This is the recommended way to set up i18n in your application. Call it once in a root component and then use I18n::get (or the t!, te!, tid! macros) in any descendant component to access translations.

See I18n::create for a manual initialization where you can also handle errors.

ยงPanics

Panics if the I18nConfig fails to produce a valid Fluent bundle.

fn app() -> impl IntoElement {
    let mut i18n = use_init_i18n(|| {
        I18nConfig::new(langid!("en-US"))
            .with_locale(Locale::new_static(
                langid!("en-US"),
                include_str!("../../../examples/i18n/en-US.ftl"),
            ))
            .with_locale(Locale::new_dynamic(
                langid!("es-ES"),
                "../../../examples/i18n/es-ES.ftl",
            ))
    });

    let change_to_spanish = move |_| i18n.set_language(langid!("es-ES"));

    rect()
        .child(t!("hello_world"))
        .child(Button::new().on_press(change_to_spanish).child("Spanish"))
}