Macro leptos_fluent::leptos_fluent

source ·
leptos_fluent!() { /* proc-macro */ }
Expand description

Create the i18n context for internationalization.

§Example

use fluent_templates::static_loader;
use leptos::*;
use leptos_fluent::leptos_fluent;

static_loader! {
    static TRANSLATIONS = {
        locales: "./locales",
        fallback_language: "en-US",
    };
}

#[component]
pub fn App() -> impl IntoView {
    leptos_fluent! {{
        translations: TRANSLATIONS,
        languages: "./locales/languages.json",
        sync_html_tag_lang: true,
        initial_language_from_url: true,
        initial_language_from_url_param: "lang",
        initial_language_from_url_to_localstorage: true,
        initial_language_from_localstorage: true,
        initial_language_from_navigator: true,
        localstorage_key: "language",
    }};

    view! {
        ...
    }
}

§Arguments

  • translations *: Translations to be used by your application. This must be the same identifier used in the fluent_templates::static_loader! macro, which returns once_cell:sync::Lazy<[StaticLoader]>.
  • locales: Path to the locales folder, which must contain the translations for each language in your application. Is expected to be a path relative from Cargo.toml file. Either locales or languages is required.
  • languages: Path to a languages file, which should be a JSON array of arrays, where each inner array contains a language identifier and a language name, respectively. The language identifier should be a valid language tag, such as en-US, en, es-ES, etc. Is expected to be a path relative from Cargo.toml file. Either locales or languages is required. For example:
    [
      ["en-US", "English (United States)"],
      ["es-ES", "Español (España)"]
    ]
    
  • sync_html_tag_lang (false): Synchronize the global <html lang="..."> attribute with current language using leptos::create_effect. Can be a literal boolean or an expression that will be evaluated at runtime.
  • initial_language_from_url (false): Load the initial language of the user from a URL parameter. Can be a literal boolean or an expression that will be evaluated at runtime. It will only take effect on client-side.
  • initial_language_from_url_param ("lang"): The parameter name to look for the initial language in the URL. Can be a literal string or an expression that will be evaluated at runtime. It will only take effect on client-side.
  • initial_language_from_url_to_localstorage (false): Save the initial language of the user from the URL to local storage. Can be a literal boolean or an expression that will be evaluated at runtime. It will only take effect on client-side.
  • initial_language_from_localstorage (false): Load the initial language of the user from local storage if not found in the URL param. Can be a literal boolean or an expression that will be evaluated at runtime. It will only take effect on client-side.
  • initial_language_from_navigator (false): Load the initial language of the user from navigator.languages if not found in local storage. Can be a literal boolean or an expression that will be evaluated at runtime. It will only take effect on client-side.
  • localstorage_key ("lang"): The local storage field to get and save the current language of the user. Can be a literal string or an expression that will be evaluated at runtime. It will only take effect on client-side.
  • initial_language_from_accept_language_header (false): Load the initial language of the user from the Accept-Language header. Can be a literal boolean or an expression that will be evaluated at runtime. It will only take effect on server-side.