Crate leptos_i18n

Source
Expand description

§About Leptos i18n

Leptos i18n is library to help with translations in a Leptos application

It loads the translations at compile time and provides checks on translation keys, interpolation keys and the selected locale.

§Learning by examples

If you want to see what Leptos i18n is capable of, check out the examples.

Details on how to run each example can be found in its README.

§In depth documentation

You can find the book on the github repo.

§A Simple Counter

Cargo.toml:

[package.metadata.leptos-i18n]
default = "en"
locales = ["en", "fr"]

./locales/en.json:

{
    "click_to_change_lang": "Click to change language",
    "click_count": "You clicked {{ count }} times",
    "click_to_inc": "Click to increment the counter"
}

./locales/fr.json:

{
    "click_to_change_lang": "Cliquez pour changez de langue",
    "click_count": "Vous avez cliqué {{ count }} fois",
    "click_to_inc": "Cliquez pour incrémenter le compteur"
}

§Rust code:

leptos_i18n::load_locales!();
use i18n::*; // `i18n` module created by the macro above
use leptos::prelude::*;

#[component]
pub fn App() -> impl IntoView {
    leptos_meta::provide_meta_context();

    view! {
        <I18nContextProvider>
            <Counter />
            <SwitchLang />
        </I18nContextProvider>
    }
}

#[component]
fn SwitchLang() -> impl IntoView {
    let i18n = use_i18n();

    let on_switch = move |_| {
        let new_lang = match i18n.get_locale() {
            Locale::en => Locale::fr,
            Locale::fr => Locale::en,
        };
        i18n.set_locale(new_lang);
    };

    view! {
        <button on:click=on_switch>{t!(i18n, click_to_change_lang)}</button>
    }
}

#[component]
fn Counter() -> impl IntoView {
    let i18n = use_i18n();

    let (counter, set_counter) = signal( 0);

    let inc = move |_| set_counter.update(|count| *count += 1);

    let count = move || counter.get();

    view! {
        <p>{t!(i18n, click_count, count)}</p>
        // Equivalent to:
        // <p>{t!(i18n, click_count, count = count)}</p>
        // Could also be wrote:
        // <p>{t!(i18n, click_count, count = move || counter.get())}</p>
        <button on:click=inc>{t!(i18n, click_to_inc)}</button>
    }
}

Re-exports§

pub use context::use_i18n_context;
pub use context::I18nContext;
pub use context::provide_i18n_context;Deprecated

Modules§

context
This module contains the I18nContext and helpers for it.
custom_provider
This module contain utilities to create custom ICU providers.
display
This module contain some helpers to format component using the td_string! macro.
formatting
This module contain traits and helper functions for formatting different kind of value based on a locale.
locale
Contain utilities for locales
plurals
This module contain utilities for plurals.
reexports
Reexports of backend libraries, mostly about formatting.

Macros§

load_locales
Look for the configuration in the cargo manifest Cargo.toml at the root of the project and load the given locales.
scope_i18n
Scope a context to the given keys
scope_locale
Scope a locale to the given keys
t
Utility macro to easily put translation in your application.
t_display
Just like the t_string! macro but return either a struct implementing Display or a &'static str instead.
t_format
Format a given value with a given formatter and return a impl IntoView.
t_format_display
Format a given value with a given formatter and return a impl Display:
t_format_string
Format a given value with a given formatter and return a String.
t_plural
Match against the plural form of a given count:
t_plural_ordinal
Match against the plural form of a given count:
t_string
Just like the t! macro but return a &'static str or a String instead of a view.
td
Just like the t! macro but instead of taking I18nContext as the first argument it takes the desired locale.
td_display
Just like the t_display! macro but takes the Locale as an argument instead of the context.
td_format
Same as the t_format! macro but takes the desired Locale as the first argument.
td_format_display
Same as the t_format_display! macro but takes the desired Locale as the first argument.
td_format_string
Same as the t_format_string! macro but takes the desired Locale as the first argument.
td_plural
Same as the t_plural! macro but takes the desired Locale as the first argument. Directly return the value instead of wrapping it in a closure.
td_plural_ordinal
Same as the t_plural_ordinal! macro but takes the desired Locale as the first argument. Directly return the value instead of wrapping it in a closure.
td_string
Just like the t_string! macro but takes the Locale as an argument instead of the context.
tu
Same as the t! macro but untracked.
tu_display
Same as the t_display! macro but untracked.
tu_format
Same as the t_format! macro but untracked.
tu_format_display
Same as the t_format_display! macro but untracked.
tu_format_string
Same as the t_format_string! macro but untracked.
tu_plural
Same as the t_plural! macro but untracked. Directly return the value instead of wrapping it in a closure.
tu_plural_ordinal
Same as the t_plural_ordinal! macro but untracked. Directly return the value instead of wrapping it in a closure.
tu_string
Same as the t_string! macro but untracked.
use_i18n_scoped
Like use_i18n but enable to scope the context:

Structs§

ConstScope
A struct that act as a marker for a scope, can be constructed as a constant and can be used to scope a context or a locale.

Enums§

Direction
Represents the direction of a script. This is computed at compile time with icu_locid_transform::LocaleDirectionality

Traits§

Locale
Trait implemented the enum representing the supported locales of the application
LocaleKeys
Trait implemented the struct representing the translation keys
Scope
Represent a scope in a locale.

Derive Macros§

IcuDataProvider
Derive the IcuDataProvider trait