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 implementingDisplay
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 aString
instead of a view. - td
- Just like the
t!
macro but instead of takingI18nContext
as the first argument it takes the desired locale. - td_
display - Just like the
t_display!
macro but takes theLocale
as an argument instead of the context. - td_
format - Same as the
t_format!
macro but takes the desiredLocale
as the first argument. - td_
format_ display - Same as the
t_format_display!
macro but takes the desiredLocale
as the first argument. - td_
format_ string - Same as the
t_format_string!
macro but takes the desiredLocale
as the first argument. - td_
plural - Same as the
t_plural!
macro but takes the desiredLocale
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 desiredLocale
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 theLocale
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§
- Const
Scope - 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
- Locale
Keys - Trait implemented the struct representing the translation keys
- Scope
- Represent a scope in a locale.
Derive Macros§
- IcuData
Provider - Derive the
IcuDataProvider
trait