### Install leptos-fluent for CSR Source: https://mondeja.github.io/leptos-fluent/latest/install.html For client-side rendering apps, install leptos-fluent with the default configuration. ```toml [dependencies] leptos-fluent = "0.3" ``` -------------------------------- ### Install leptos-fluent for Desktop Applications Source: https://mondeja.github.io/leptos-fluent/latest/install.html To use leptos-fluent on non-wasm targets like desktop applications, install it and enable the `system` feature. ```toml [dependencies] leptos-fluent = { version = "0.3", features = ["system"] } ``` -------------------------------- ### Example Languages File (JSON5) Source: https://mondeja.github.io/leptos-fluent/latest/print.html An example of a languages file in JSON5 format, defining language codes and their corresponding display names. ```json5 // ./locales/languages.json5 [ ["en", "English"], ["es-ES", "EspaΓ±ol (EspaΓ±a)"] ] ``` -------------------------------- ### Fluent Translation File Example Source: https://mondeja.github.io/leptos-fluent/latest/print.html Example content for a main.ftl translation file. ```ftl # locales/en/main.ftl select-a-language = Select a language: language-selected-is = The selected language is { $lang }. ``` -------------------------------- ### Install leptos-fluent for SSR with Axum Source: https://mondeja.github.io/leptos-fluent/latest/install.html For server-side rendering apps, install leptos-fluent and enable the `ssr` and `axum` features. Ensure `axum` is available as a dependency. ```toml [dependencies] leptos-fluent = "0.3" axum = { version = "0.8", optional = true } [features] ssr = [ "leptos-fluent/ssr", "leptos-fluent/axum", # actix and axum are supported "dep:axum", ] ``` -------------------------------- ### Configure System Data Files for Desktop Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Example configuration for desktop applications to persist language settings in a local data file. ```rust leptos_fluent! { locales: "./locales", initial_language_from_system: true, initial_language_from_system_to_data_file: true, initial_language_from_data_file: true, set_language_to_data_file: true, data_file_key: "system-language-example", } ``` -------------------------------- ### Implement I18n Provider and Components Source: https://mondeja.github.io/leptos-fluent/latest/print.html Setup the I18nProvider and a language selector component in a Leptos application. ```rust // src/lib.rs use leptos::prelude::*; use leptos_fluent::{I18n, leptos_fluent, move_tr, Language}; #[component] pub fn I18nProvider(children: Children) -> impl IntoView { leptos_fluent! { children: children(), locales: "./locales", default_language: "en", } } #[component] pub fn App() -> impl IntoView { view! { } } #[component] fn LanguageSelector() -> impl IntoView { let i18n = expect_context::(); view! {

{move_tr!("select-a-language")}

{move || { i18n.languages.iter().map(|lang| render_language(lang)).collect::>() }}

{move_tr!( "language-selected-is", { "lang" => i18n.language.get().name } )}

} } fn render_language(lang: &'static Language) -> impl IntoView { let i18n = expect_context::(); // Passed as atrribute, `Language` is converted to their code, // so ` } } ``` -------------------------------- ### Initialize leptos-fluent in Component Source: https://mondeja.github.io/leptos-fluent/latest/languages.html Example of initializing the i18n context within a Leptos component using a custom languages file. ```rust #[component] pub fn App() -> impl IntoView { leptos_fluent! { languages: "./locales/languages.json5", default_language: "en", }; } ``` ```json5 // ./locales/languages.json5 [ ["en", "English"], ["es-ES", "EspaΓ±ol (EspaΓ±a)"], ] ``` -------------------------------- ### Configure CSR with Local Storage Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Example configuration for client-side rendering using local storage to persist language preferences. ```rust leptos_fluent! { locales: "./locales", set_language_to_local_storage: true, initial_language_from_local_storage: true, initial_language_from_navigator: true, initial_language_from_navigator_to_local_storage: true, initial_language_from_url_param: true, initial_language_from_url_param_to_local_storage: true, local_storage_key: "lang", } ``` -------------------------------- ### Advanced if-else patterns with arguments Source: https://mondeja.github.io/leptos-fluent/latest/advanced-usage.html Examples showing how to use conditional logic with arguments in stable and nightly Rust. ```rust // stable #[allow(unused_parens)] _ = tr!( i18n, if (my_signal.get() && my_function()) {"foo"} else {"bar"}, { "arg1" => "value1", "arg2" => "value2", } ); // nightly #![feature(stmt_expr_attributes)] _ = move_tr!( i18n, #[allow(unused_parens)] if (my_signal.get() && my_function()) {"foo"} else {"bar"}, #[allow(unused_braces)] { "arg1" => "value1", "arg2" => {"value2"}, } ); ``` -------------------------------- ### Handle Reactive Context Warnings Source: https://mondeja.github.io/leptos-fluent/latest/print.html Example of a console warning when accessing signals outside a reactive context. ```text At `./path/to/file.rs:ln`, you access a signal or memo (defined at `./path/to/file.rs:ln`) outside of a reactive context. This might mean your app is not responding to changes in signal values in the way you expect. ``` -------------------------------- ### Define Fluent Locales Source: https://mondeja.github.io/leptos-fluent/latest/print.html Example content for a Fluent locale file. ```fluent select-a-language = Selecciona un idioma: language-selected-is = El idioma seleccionado es { $lang }. ``` -------------------------------- ### Configure CSR and SSR with Cookies Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Example configuration for hybrid rendering using cookies to synchronize language settings between client and server. ```rust leptos_fluent! { locales: "./locales", set_language_to_cookie: true, initial_language_from_cookie: true, initial_language_from_navigator: true, initial_language_from_navigator_to_cookie: true, initial_language_from_url_param: true, initial_language_from_url_param_to_cookie: true, initial_language_from_accept_language_header: true, cookie_name: "lf-lang", } ``` -------------------------------- ### Server Action with Client-Side Translations Source: https://mondeja.github.io/leptos-fluent/latest/faqs.html Example of a server action that receives and logs a client-side translated message. Translations are passed as values due to client-side storage. ```rust use leptos::prelude::*; use leptos_fluent::{tr, Language, I18n}; /// Server action showing client-side translated message on console #[server(ShowHelloWorld, "/api")] pub async fn show_hello_world( translated_hello_world: String, language: String, ) -> Result<(), ServerFnError> { println!("{translated_hello_world} ({language})"); Ok(()) } fn render_language(lang: &'static Language) -> impl IntoView { let i18n = expect_context::(); // Call on click to server action with a client-side translated // "hello-world" message let on_click = move |_| { i18n.language.set(lang); spawn_local(async { _ = show_hello_world( tr!("hello-world"), lang.name.to_string(), ).await; }); }; view! {
} } ``` -------------------------------- ### Demonstrate panic when using tr! in event listeners Source: https://mondeja.github.io/leptos-fluent/latest/advanced-usage.html This example illustrates code that will panic in Leptos v0.7 when triggered by an event listener due to missing context. ```rust use leptos::prelude::*; use leptos_fluent::tr; #[component] pub fn App() -> impl IntoView { view! { } } #[component] pub fn Child() -> impl IntoView { view! {
"CLICK ME!"
} } ``` -------------------------------- ### Initialize Language from URL Parameter to Local Storage Source: https://mondeja.github.io/leptos-fluent/latest/print.html Use `initial_language_from_url_param_to_local_storage` to get the initial language from the URL parameter and save it to local storage. This is a CSR-only feature. ```rust leptos_fluent! { // ... initial_language_from_url_param_to_local_storage: true, } ``` -------------------------------- ### Initialize Language from URL Parameter to Session Storage Source: https://mondeja.github.io/leptos-fluent/latest/print.html Use `initial_language_from_url_param_to_session_storage` to get the initial language from the URL parameter and save it to session storage. This is a CSR-only feature. ```rust leptos_fluent! { // ... initial_language_from_url_param_to_session_storage: true, } ``` -------------------------------- ### Get Initial Language from Server Function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Configure Leptos Fluent to fetch the initial language setting from a server function. The server function must return a `Result, ServerFnError>`. ```rust __ leptos_fluent! { // ... initial_language_from_server_function: initial_language_server_function, } /// Server function to set the initial language #[server(InitialLanguage, "/api")] pub async fn initial_language_server_function( ) -> Result, ServerFnError> { // .. replace with your own logic Ok(Some("es".to_string())) } ``` -------------------------------- ### Configure language sources and targets Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Demonstrates setting up initial language sources from the system and syncing with a data file. ```rust leptos_fluent! { // .. // Get the initial language from the source operative system initial_language_from_system: true, // and set to the target file. initial_language_from_system_to_data_file: true, // If a target data file exists, get the initial language from it. initial_language_from_data_file: true, // When the language is updated, set it to the file. set_language_to_data_file: true, // Unique file name to set the language for this app: data_file_key: "system-language-example", }; ``` -------------------------------- ### Mount Application Source: https://mondeja.github.io/leptos-fluent/latest/print.html Entry point for the Leptos application. ```rust // src/main.rs pub fn main() { console_error_panic_hook::set_once(); leptos::mount::mount_to_body(minimal_example::App); } ``` -------------------------------- ### Get Current Language Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html Retrieve the current active language using the `get` method of the `language` field. Requires `I18n` to be in the context. ```rust use leptos::prelude::*; use leptos_fluent::I18n; let i18n = expect_context::(); let lang = i18n.language.get(); ``` -------------------------------- ### leptos_fluent! Configuration Source: https://mondeja.github.io/leptos-fluent/latest/print.html Configuration options for language initialization and persistence settings. ```APIDOC ## leptos_fluent! Configuration ### Description Configure how the application initializes and persists the user's language preference across different storage backends. ### Parameters #### Request Body - **initial_language_from_local_storage_to_cookie** (bool) - Optional - Syncs language from local storage to cookie. - **initial_language_from_local_storage_to_session_storage** (bool) - Optional - Syncs language from local storage to session storage. - **set_language_to_local_storage** (bool) - Optional - Enables saving current language to local storage. - **session_storage_key** (string) - Optional - Key used for session storage (default: "lang"). - **initial_language_from_session_storage** (bool) - Optional - Initializes language from session storage. - **initial_language_from_session_storage_to_cookie** (bool) - Optional - Syncs language from session storage to cookie. - **initial_language_from_session_storage_to_local_storage** (bool) - Optional - Syncs language from session storage to local storage. - **set_language_to_session_storage** (bool) - Optional - Enables saving current language to session storage. - **initial_language_from_navigator** (bool) - Optional - Initializes language from browser navigator. - **initial_language_from_navigator_to_local_storage** (bool) - Optional - Syncs navigator language to local storage. - **initial_language_from_navigator_to_session_storage** (bool) - Optional - Syncs navigator language to session storage. - **initial_language_from_navigator_to_cookie** (bool) - Optional - Syncs navigator language to cookie. - **set_language_from_navigator** (bool) - Optional - Updates language when browser settings change. - **initial_language_from_accept_language_header** (bool) - Optional - Initializes language from SSR Accept-Language header. - **cookie_name** (string) - Optional - Name of the language cookie (default: "lf-lang"). - **cookie_attrs** (string) - Optional - Attributes for the language cookie. - **initial_language_from_cookie** (bool) - Optional - Initializes language from cookie. - **initial_language_from_cookie_to_local_storage** (bool) - Optional - Syncs cookie language to local storage. - **initial_language_from_cookie_to_session_storage** (bool) - Optional - Syncs cookie language to session storage. - **set_language_to_cookie** (bool) - Optional - Enables saving current language to cookie. - **initial_language_from_system** (bool) - Optional - Initializes language from system. - **initial_language_from_data_file** (bool) - Optional - Initializes language from data file. - **initial_language_from_system_to_data_file** (bool) - Optional - Syncs system language to data file. ### Request Example leptos_fluent! { initial_language_from_local_storage_to_cookie: true, session_storage_key: "lang", cookie_name: "lang" } ``` -------------------------------- ### Get Current Language Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html Shows how to retrieve the current active language using the `get` method or by calling the context directly when the `nightly` feature is enabled. ```APIDOC ## Get Current Language ### Description To get the current active language, use `get` method of `language` field. ### Method `i18n.language.get()` ### Request Example ```rust use leptos::prelude::*; use leptos_fluent::I18n; let i18n = expect_context::(); let lang = i18n.language.get(); ``` ### Nightly Feature With `nightly` feature enabled, can get the active language calling the context as a function. ### Method `i18n()` ### Request Example ```rust use leptos::prelude::*; use leptos_fluent::I18n; let i18n = expect_context::(); let lang = i18n(); ``` ``` -------------------------------- ### System: Load initial language from system to data file Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Fetches the initial language from the system settings and saves it into a data file. This synchronizes system preferences with a file-based configuration. ```Rust leptos_fluent! { // ... initial_language_from_system_to_data_file: true, } ``` -------------------------------- ### Get Active Language Source: https://mondeja.github.io/leptos-fluent/latest/print.html Retrieving the current active language. ```rust use leptos::prelude::*; use leptos_fluent::I18n; let i18n = expect_context::(); let lang = i18n.language.get(); ``` ```rust use leptos::prelude::*; use leptos_fluent::I18n; let i18n = expect_context::(); let lang = i18n(); ``` -------------------------------- ### Get Available Languages Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html Explains how to access the list of available languages. ```APIDOC ## Get Available Languages ### Description To get the available languages, iterate over the `languages` field. ### Method `i18n.languages.iter()` ### Request Example ```rust i18n.languages.iter() ``` ``` -------------------------------- ### Initial language from storage or browser to server function Source: https://mondeja.github.io/leptos-fluent/latest/print.html Syncs initial language from various sources to a server function. ```rust leptos_fluent! { // ... initial_language_from_local_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ```rust leptos_fluent! { // ... initial_language_from_session_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ```rust leptos_fluent! { // ... initial_language_from_cookie_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ```rust leptos_fluent! { // ... initial_language_from_navigator_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ```rust leptos_fluent! { // ... initial_language_from_url_param_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ```rust leptos_fluent! { // ... initial_language_from_url_path_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` -------------------------------- ### Main Function for Leptos App Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html Sets up the panic hook and mounts the Leptos application to the body. ```rust pub fn main() { console_error_panic_hook::set_once(); leptos::mount::mount_to_body(minimal_example::App); } ``` -------------------------------- ### Configure Initial Language Loading Source: https://mondeja.github.io/leptos-fluent/latest/print.html Set up initial language detection from the system and data files, and ensure language updates are written to the data file. ```rust leptos_fluent! { // .. // Get the initial language from the source operative system initial_language_from_system: true, // and set to the target file. initial_language_from_system_to_data_file: true, // If a target data file exists, get the initial language from it. initial_language_from_data_file: true, // When the language is updated, set it to the file. set_language_to_data_file: true, // Unique file name to set the language for this app: data_file_key: "system-language-example", }; ``` -------------------------------- ### Spanish Translations (FTL) Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html Example Fluent translation file for Spanish, defining messages for language selection. ```ftl select-a-language = Selecciona un idioma: language-selected-is = El idioma seleccionado es { $lang }. ``` -------------------------------- ### English Translations (FTL) Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html Example Fluent translation file for English, defining messages for language selection. ```ftl select-a-language = Select a language: language-selected-is = The selected language is { $lang }. ``` -------------------------------- ### Initial Language from Various Sources to Server Function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Configure fetching the initial language from different client-side storage or browser APIs and sending it to a server function. ```APIDOC ## Initial Language from Client to Server Function ### `initial_language_from_local_storage_to_server_function` #### Description Get the initial language from local storage and set it to a server function. #### Usage ```rust leptos_fluent! { // ... initial_language_from_local_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### `initial_language_from_session_storage_to_server_function` #### Description Get the initial language from session storage and set it to a server function. #### Usage ```rust leptos_fluent! { // ... initial_language_from_session_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### `initial_language_from_cookie_to_server_function` #### Description Get the initial language from a cookie and set it to a server function. #### Usage ```rust leptos_fluent! { // ... initial_language_from_cookie_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### `initial_language_from_navigator_to_server_function` #### Description Get the initial language from `navigator.languages` and set it to a server function. #### Usage ```rust leptos_fluent! { // ... initial_language_from_navigator_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### `initial_language_from_url_param_to_server_function` #### Description Get the initial language from a URL parameter and set it to a server function. #### Usage ```rust leptos_fluent! { // ... initial_language_from_url_param_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### `initial_language_from_url_path_to_server_function` #### Description Get the initial language from a URL path and set it to a server function. #### Usage ```rust leptos_fluent! { // ... initial_language_from_url_path_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ``` -------------------------------- ### initial_language_from_local_storage_to_server_function Source: https://mondeja.github.io/leptos-fluent/latest/print.html Initializes the language from local storage and sets it via a server function. ```APIDOC ## `initial_language_from_local_storage_to_server_function` ### Description Get the initial language from local storage and set it to a server function. ### Method Configuration Option ### Endpoint N/A ### Parameters - **initial_language_from_local_storage_to_server_function** (function identifier) - Required - The server function to call. It should accept a language string and return `Result<(), ServerFnError>`. ### Request Example ```rust leptos_fluent! { // ... initial_language_from_local_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### Response N/A ``` -------------------------------- ### Generated Languages Array Structure Source: https://mondeja.github.io/leptos-fluent/latest/languages.html Example of the Rust structure generated by leptos-fluent at compile time based on the locales directory. ```rust let LANGUAGES = [ leptos_fluent::Language { id: "en", name: "English", dir: leptos_fluent::WritingDirection::Ltr, flag: Some("πŸ‡¬πŸ‡§"), script: None, }, leptos_fluent::Language { id: "es-ES", name: "EspaΓ±ol (EspaΓ±a)", dir: leptos_fluent::WritingDirection::Ltr, flag: Some("πŸ‡ͺπŸ‡Έ"), script: None, }, ] ``` -------------------------------- ### leptos_fluent! Macro Configuration Source: https://mondeja.github.io/leptos-fluent/latest/print.html This snippet shows the basic configuration options for the `leptos_fluent!` macro, including setting initial language sources and managing language data files. ```APIDOC ## leptos_fluent! Macro Configuration ### Description Configure the initial language loading behavior and how language data is managed. ### Parameters #### `initial_language_from_system` (boolean) - Required: No - Description: Get the initial language from the source operative system. #### `initial_language_from_system_to_data_file` (boolean) - Required: No - Description: Set the initial language obtained from the system to the data file. #### `initial_language_from_data_file` (boolean) - Required: No - Description: If a target data file exists, get the initial language from it. #### `set_language_to_data_file` (boolean) - Required: No - Description: When the language is updated, set it to the data file. #### `data_file_key` (string) - Required: No - Description: Unique file name to set the language for this app. ### Request Example ```rust leptos_fluent! { initial_language_from_system: true, initial_language_from_system_to_data_file: true, initial_language_from_data_file: true, set_language_to_data_file: true, data_file_key: "system-language-example", }; ``` ``` -------------------------------- ### Get Current Language (Nightly Feature) Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html With the `nightly` feature enabled, the active language can be retrieved by calling the context as a function. ```rust use leptos::prelude::*; use leptos_fluent::I18n; let i18n = expect_context::(); let lang = i18n(); ``` -------------------------------- ### Use conditional translation keys Source: https://mondeja.github.io/leptos-fluent/latest/print.html Examples of using if/else expressions within translation macros to allow compile-time checking of keys. ```rust use leptos_fluent::{tr, move_tr}; let (foo, bar) = (false, true); tr!(if foo {"foo"} else {"bar"}); move_tr!(if foo {"foo"} else if bar {"bar"} else {"baz"}); ``` ```rust // stable #[allow(unused_parens)] _ = tr!( i18n, if (my_signal.get() && my_function()) {"foo"} else {"bar"}, { "arg1" => "value1", "arg2" => "value2", } ); // nightly #![feature(stmt_expr_attributes)] _ = move_tr!( i18n, #[allow(unused_parens)] if (my_signal.get() && my_function()) {"foo"} else {"bar"}, #[allow(unused_braces)] { "arg1" => "value1", "arg2" => {"value2"}, } ); ``` -------------------------------- ### Enable Tracing Support Source: https://mondeja.github.io/leptos-fluent/latest/install.html To enable `tracing` support, add the `tracing` feature to `leptos-fluent` and include `fluent-templates` in your dependencies. ```toml [dependencies] leptos-fluent = { version = "0.3", features = ["tracing"] } fluent-templates = "0.13" ``` -------------------------------- ### Meta Context Provision Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Enable providing macro meta information at runtime as a context. ```APIDOC ## Provide Meta Context ### `provide_meta_context` #### Description Provide the macro meta information at runtime as a context. This can be retrieved using `I18n::meta`. #### Usage ```rust use leptos::prelude::*; use leptos_fluent::{I18n, leptos_fluent}; leptos_fluent! { // ... provide_meta_context: true, } let i18n = leptos::prelude::expect_context::(); println!("Macro parameters: {:?}", i18n.meta().unwrap()); ``` ``` -------------------------------- ### Project Structure for Minimal Skeleton Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html This outlines the basic file and directory structure for a leptos-fluent project, including locale files. ```treeview . β”œβ”€β”€ πŸ“„ Cargo.toml β”œβ”€β”€ πŸ“ locales β”‚ β”œβ”€β”€ πŸ“ en β”‚ β”‚ └── πŸ“„ main.ftl β”‚ └── πŸ“ es β”‚ └── πŸ“„ main.ftl └── πŸ“ src β”œβ”€β”€ πŸ“„ main.rs └── πŸ“„ lib.rs ``` -------------------------------- ### Initialize Language from URL Parameter to Cookie Source: https://mondeja.github.io/leptos-fluent/latest/print.html Use `initial_language_from_url_param_to_cookie` to get the initial language from the URL parameter and save it to a cookie. This is a CSR-only feature. ```rust leptos_fluent! { // ... initial_language_from_url_param_to_cookie: true, } ``` -------------------------------- ### initial_language_from_url_path_to_server_function Source: https://mondeja.github.io/leptos-fluent/latest/print.html Initializes the language from a URL path and sets it via a server function. ```APIDOC ## `initial_language_from_url_path_to_server_function` ### Description Get the initial language from a URL path and set it to a server function. ### Method Configuration Option ### Endpoint N/A ### Parameters - **initial_language_from_url_path_to_server_function** (function identifier) - Required - The server function to call. It should accept a language string and return `Result<(), ServerFnError>`. ### Request Example ```rust leptos_fluent! { // ... initial_language_from_url_path_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### Response N/A ``` -------------------------------- ### Initial Language from Server Function to Cookie Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Configure Leptos Fluent to get the initial language from a server function and then store it in a cookie. This is a boolean flag. ```rust __ leptos_fluent! { // ... initial_language_from_server_function_to_cookie: true, } ``` -------------------------------- ### System: Load initial language from data file Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Loads the initial language preference from a specified data file. This allows for language configuration to be managed externally in a file. ```Rust leptos_fluent! { // ... initial_language_from_data_file: true, } ``` -------------------------------- ### System Language Configuration with Data Files Source: https://mondeja.github.io/leptos-fluent/latest/print.html Configures leptos_fluent! for system-level language management using data files, suitable for desktop applications. It handles initialization from system settings and data files, and allows setting language to data files. ```rust leptos_fluent! { locales: "./locales", initial_language_from_system: true, initial_language_from_system_to_data_file: true, initial_language_from_data_file: true, set_language_to_data_file: true, data_file_key: "system-language-example", } ``` -------------------------------- ### CSR: Load initial language from navigator to cookie Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Gets the initial language from `navigator.languages` and saves it into a cookie. This respects browser settings and persists the choice. ```Rust leptos_fluent! { // ... initial_language_from_navigator_to_cookie: true, } ``` -------------------------------- ### initial_language_from_navigator_to_server_function Source: https://mondeja.github.io/leptos-fluent/latest/print.html Initializes the language from `navigator.languages` and sets it via a server function. ```APIDOC ## `initial_language_from_navigator_to_server_function` ### Description Get the initial language from `navigator.languages` and set it to a server function. ### Method Configuration Option ### Endpoint N/A ### Parameters - **initial_language_from_navigator_to_server_function** (function identifier) - Required - The server function to call. It should accept a language string and return `Result<(), ServerFnError>`. ### Request Example ```rust leptos_fluent! { // ... initial_language_from_navigator_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### Response N/A ``` -------------------------------- ### Set supported languages file Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Points to a JSON file containing the list of supported languages. ```rust leptos_fluent! { locales: "./locales", languages: "./locales/languages.json", // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } ``` -------------------------------- ### Provide meta context Source: https://mondeja.github.io/leptos-fluent/latest/print.html Exposes macro meta information at runtime via context. ```rust use leptos::prelude::*; use leptos_fluent::{I18n, leptos_fluent}; leptos_fluent! { // ... provide_meta_context: true, } let i18n = leptos::prelude::expect_context::(); println!("Macro parameters: {:?}", i18n.meta().unwrap()); ``` -------------------------------- ### Manage language via local storage Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Configure the local storage key and enable initial language retrieval from local storage. ```rust leptos_fluent! { // ... local_storage_key: "lang", } ``` ```rust leptos_fluent! { // ... initial_language_from_local_storage: true, } ``` -------------------------------- ### Initial Language from Local Storage to Server Function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Set the initial language by reading from local storage and then sending it to a server function. This uses the `set_language_server_function`. ```rust __ leptos_fluent! { // ... initial_language_from_local_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` -------------------------------- ### Conditional Configuration for leptos_fluent! Source: https://mondeja.github.io/leptos-fluent/latest/advanced-usage.html Use conditional compilation directives like `#[cfg(debug_assertions)]` to set macro parameters differently for debug and release builds. This example shows how to control the `set_language_to_url_param` setting. ```rust leptos_fluent! { // ... #[cfg(debug_assertions)] set_language_to_url_param: true, #[cfg(not(debug_assertions))] set_language_to_url_param: false, } ``` -------------------------------- ### System: Load initial language from system Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Retrieves the initial language preference directly from the operating system's settings. This provides a system-level default for the application's language. ```Rust leptos_fluent! { // ... initial_language_from_system: true, } ``` -------------------------------- ### Manage language via URL parameters Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Configure URL parameter settings for language management and persistence. ```rust leptos_fluent! { // ... url_param: "lang", } ``` ```rust leptos_fluent! { // ... initial_language_from_url_param: true, } ``` ```rust leptos_fluent! { // ... initial_language_from_url_param_to_local_storage: true, } ``` ```rust leptos_fluent! { // ... initial_language_from_url_param_to_session_storage: true, } ``` ```rust leptos_fluent! { // ... initial_language_from_url_param_to_cookie: true, } ``` ```rust leptos_fluent! { // ... set_language_to_url_param: true, } ``` -------------------------------- ### CSR: Load initial language from navigator to local storage Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Retrieves the initial language from `navigator.languages` and saves it into local storage for persistence. ```Rust leptos_fluent! { // ... initial_language_from_navigator_to_local_storage: true, } ``` -------------------------------- ### Configure initial language from server function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Enables the retrieval of the initial language from a server function and its subsequent storage in local storage. ```rust leptos_fluent! { // ... initial_language_from_server_function_to_local_storage: true, } ``` -------------------------------- ### Basic CSR Project Structure Source: https://mondeja.github.io/leptos-fluent/latest/print.html Standard directory layout for a minimal CSR application using leptos-fluent. ```text . β”œβ”€β”€ πŸ“„ Cargo.toml β”œβ”€β”€ πŸ“ locales β”‚ β”œβ”€β”€ πŸ“ en β”‚ β”‚ └── πŸ“„ main.ftl β”‚ └── πŸ“ es β”‚ └── πŸ“„ main.ftl └── πŸ“ src β”œβ”€β”€ πŸ“„ main.rs └── πŸ“„ lib.rs ``` -------------------------------- ### Initial Language from Navigator to Server Function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Set the initial language by reading from `navigator.languages` and then sending it to a server function. This uses the `set_language_server_function`. ```rust __ leptos_fluent! { // ... initial_language_from_navigator_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` -------------------------------- ### initial_language_from_session_storage_to_server_function Source: https://mondeja.github.io/leptos-fluent/latest/print.html Initializes the language from session storage and sets it via a server function. ```APIDOC ## `initial_language_from_session_storage_to_server_function` ### Description Get the initial language from session storage and set it to a server function. ### Method Configuration Option ### Endpoint N/A ### Parameters - **initial_language_from_session_storage_to_server_function** (function identifier) - Required - The server function to call. It should accept a language string and return `Result<(), ServerFnError>`. ### Request Example ```rust leptos_fluent! { // ... initial_language_from_session_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### Response N/A ``` -------------------------------- ### Initial Language from URL Param to Server Function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Set the initial language by reading from a URL parameter and then sending it to a server function. This uses the `set_language_server_function`. ```rust __ leptos_fluent! { // ... initial_language_from_url_param_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` -------------------------------- ### Initialize Language from URL Path to Local Storage Source: https://mondeja.github.io/leptos-fluent/latest/print.html Use `initial_language_from_url_path_to_local_storage` to set the initial language from the URL path and store it in local storage. This is a CSR-only feature. ```rust leptos_fluent! { // ... initial_language_from_url_path_to_local_storage: true, } ``` -------------------------------- ### initial_language_from_url_param_to_server_function Source: https://mondeja.github.io/leptos-fluent/latest/print.html Initializes the language from a URL parameter and sets it via a server function. ```APIDOC ## `initial_language_from_url_param_to_server_function` ### Description Get the initial language from a URL parameter and set it to a server function. ### Method Configuration Option ### Endpoint N/A ### Parameters - **initial_language_from_url_param_to_server_function** (function identifier) - Required - The server function to call. It should accept a language string and return `Result<(), ServerFnError>`. ### Request Example ```rust leptos_fluent! { // ... initial_language_from_url_param_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` ### Response N/A ``` -------------------------------- ### Accessing Macro Parameters at Runtime Source: https://mondeja.github.io/leptos-fluent/latest/faqs.html Demonstrates how to access parameters provided to the `leptos_fluent!` macro at runtime using `provide_meta_context` and `I18n::meta`. ```rust leptos_fluent! { // ... provide_meta_context: true, }; // ... later let i18n = expect_context::(); println!("Macro parameters: {:?}", i18n.meta().unwrap()); ``` -------------------------------- ### Initial Language from Session Storage to Server Function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Set the initial language by reading from session storage and then sending it to a server function. This uses the `set_language_server_function`. ```rust __ leptos_fluent! { // ... initial_language_from_session_storage_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` -------------------------------- ### Configure leptos-fluent for SSR Source: https://mondeja.github.io/leptos-fluent/latest/print.html Enable SSR features and integrate with web frameworks like Axum, while configuring cargo-leptos to watch locale files. ```toml [dependencies] leptos-fluent = "0.3" axum = { version = "0.8", optional = true } [features] ssr = [ "leptos-fluent/ssr", "leptos-fluent/axum", # actix and axum are supported "dep:axum", ] # Using cargo-leptos [package.metadata.leptos] watch-additional-files = ["locales"] ``` -------------------------------- ### Provide Meta Context for I18n Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Enable providing macro meta information as a context at runtime. This allows retrieval using `I18n::meta`. ```rust __ use leptos::prelude::* use leptos_fluent::{I18n, leptos_fluent}; leptos_fluent! { // ... provide_meta_context: true, } let i18n = leptos::prelude::expect_context::(); println!("Macro parameters: {:?}", i18n.meta().unwrap()); ``` -------------------------------- ### Configure data file key Source: https://mondeja.github.io/leptos-fluent/latest/print.html Sets a unique key for managing the language in the data file. ```rust leptos_fluent! { // ... data_file_key: "my-app", } ``` -------------------------------- ### Initial Language from URL Path to Server Function Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Set the initial language by reading from a URL path segment and then sending it to a server function. This uses the `set_language_server_function`. ```rust __ leptos_fluent! { // ... initial_language_from_url_path_to_server_function: set_language_server_function, } #[server(SetLanguage, "/api")] pub async fn set_language_server_function( language: String, ) -> Result<(), ServerFnError> { // .. replace with your own logic Ok(()) } ``` -------------------------------- ### Initial Language from Local Storage Source: https://mondeja.github.io/leptos-fluent/latest/print.html Enables the application to load the initial language from local storage. This provides a persistent language selection across user sessions. ```APIDOC ## `initial_language_from_local_storage` ### Description Get initial language from local storage. ### Method Configuration Option ### Endpoint N/A (Macro configuration) ### Parameters #### Request Body - **initial_language_from_local_storage** (boolean) - Optional - Set to `true` to enable loading from local storage. ### Request Example ```rust leptos_fluent! { // ... initial_language_from_local_storage: true, } ``` ### Response N/A (Compile-time configuration) ``` -------------------------------- ### Cargo.toml Configuration for Leptos Fluent Source: https://mondeja.github.io/leptos-fluent/latest/basic-usage.html Defines project metadata, dependencies including leptos and leptos-fluent, and Leptos-specific build configurations. ```toml [package] name = "minimal-example" edition = "2021" version = "0.1.0" [lib] name = "minimal_example" path = "src/lib.rs" [dependencies] leptos = { version = "0.8", features = ["csr"] } leptos-fluent = "0.3" console_error_panic_hook = "0.1" # Using cargo-leptos [package.metadata.leptos] watch-additional-files = ["locales"] ``` -------------------------------- ### Destructure leptos_fluent parameters Source: https://mondeja.github.io/leptos-fluent/latest/print.html Shows how to use shorthand syntax for macro parameters and how to pass them through component props. ```rust let my_cookie_name = "language"; leptos_fluent! { // ... cookie_name: my_cookie_name, } ``` ```rust let cookie_name = "language"; leptos_fluent! { // ... cookie_name, // `cookie_name: cookie_name` } ``` ```rust #[component] pub fn I18n(children: Children, cookie_name: &str) -> impl IntoView { leptos_fluent! { children: children(), // ... cookie_name, } } #[component] pub fn App() -> impl IntoView { view! { // ... } } ``` -------------------------------- ### Server Function Integration for Language Initialization Source: https://mondeja.github.io/leptos-fluent/latest/leptos_fluent.html Configure how the initial language is fetched from a server function. ```APIDOC ## Initial Language from Server Function ### `initial_language_from_server_function` #### Description Get the initial language from a server function. The server function must return a `Result, ServerFnError>`. #### Parameters - **`initial_language_from_server_function`** (identifier) - Required - An identifier to the server function that will be called to get the initial language. #### Usage ```rust leptos_fluent! { // ... initial_language_from_server_function: initial_language_server_function, } /// Server function to set the initial language #[server(InitialLanguage, "/api")] pub async fn initial_language_server_function( ) -> Result, ServerFnError> { // .. replace with your own logic Ok(Some("es".to_string())) } ``` ```