Module freya::_docs::i18n

source ·
Expand description

§i18n

You may add i18n (localization) support to your Freya app by using the dioxus-i18n crate.

# en-US.ftl

hello_world = Hello, World!
hello = Hello, {$name}!
# es-ES.ftl

hello_world = Hola, Mundo!
hello = Hola, {$name}!

// main.rs

fn main() {
    launch(app);
}

#[allow(non_snake_case)]
fn Body() -> Element {
    // Access to the i18n state
    let mut i18n = i18n();

    // Update the current language
    let change_to_english = move |_| i18n.set_language(langid!("en-US"));
    let change_to_spanish = move |_| i18n.set_language(langid!("es-ES"));

    rsx!(
        rect {
            rect {
                direction: "horizontal",
                Button {
                    onclick: change_to_english,
                    label {
                        "English"
                    }
                }
                Button {
                    onclick: change_to_spanish,
                    label {
                        "Spanish"
                    }
                }
            }

            // Get and subscribe to these messages
            label { { t!("hello_world") } }
            label { { t!("hello", name: "Dioxus") } }
        }
    )
}

fn app() -> Element {
    // Initialize our i18n config
    use_init_i18n(|| {
        I18nConfig::new(langid!("en-US"))
            .with_locale(Locale::new_static(
                langid!("en-US"),
                include_str!("./en-US.ftl"),
            ))
            .with_locale(Locale::new_dynamic(
                langid!("es-ES"),
                "./es-ES.ftl",
            ))
    });

    rsx!(Body {})
}