FAQs
- How to get
LanguageIdentifier
if not installed? - How to get the i18n context at initialization?
- Custom cookie attributes are invalid
- How to get the fallback language
- Why examples don't use
<For/>
component? - How to manage translations on server actions
- How to change
<html>
attributes on SSR - How to get values of
leptos_fluent!
macro at runtime? - Different configuration conditional checks
How to get LanguageIdentifier
if not installed?
#![allow(unused)] fn main() { use fluent_templates::LanguageIdentifier; }
fluent-templates
also depends externally on
fluent-bundle
whichs provides utilities for parsing the Fluent syntax.
How to get the i18n context at initialization?
#![allow(unused)] fn main() { use leptos_fluent::leptos_fluent; let i18n = leptos_fluent! {{ // ... }}; leptos::logging::log!("i18n context: {i18n:?}"); }
Custom cookie attributes are invalid
Use an expression to set the cookie attributes and will not be validated.
#![allow(unused)] fn main() { let attrs = "SameSite=Strict; MyCustomAttr=MyCustomValue;"; leptos_fluent! {{ cookie_attrs: attrs, // ... }} }
How to get the fallback language
From fluent-templates v0.9.5
onwards can be obtained from your translations.
#![allow(unused)] fn main() { let fallback_language = expect_i18n().translations.get()[0].fallback(); }
Why examples don't use <For/>
component?
There are some cases in which the <For/>
component is not reproducible between
SSR and hydrate modes leading to different renders, so decided to use a
simple vector to not bring confusion to main examples.
In any case, the <For/>
component is secure on CSR contexts and
leptos_fluent::Language
s implement Hash
and Eq
traits to be
able to be passed directly to key
s properties trigerring reactivity
depending on the current active language.
#![allow(unused)] fn main() { use leptos_fluent::{i18n, Language}; leptos::logging::warn!("[WARNING]: Not secure on SSR"); view! { <p>{move_tr!("select-a-language")}</p> <For each=move || i18n().languages key=|lang| *lang children=move |lang| render_language(lang) /> } fn render_language(lang: &'static Language) -> impl IntoView { ... } }
How to manage translations on server actions
The translations reside on the client side, so the I18n
can not be
accessed as context on server actions. Pass the translations as values
if the bandwidth is not a problem or use your own statics on server side.
#![allow(unused)] fn main() { use leptos::*; use leptos_fluent::{tr, Language}; /// 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 { // Call on click to server action with a client-side translated // "hello-world" message let on_click = move |_| { lang.activate(); spawn_local(async { _ = show_hello_world( tr!("hello-world"), lang.name.to_string(), ).await; }); }; view! { <div> <label for=lang>{lang.name}</label> <input id=lang name="language" value=lang checked=lang.is_active() on:click=on_click type="radio" /> </div> } } }
How to change <html>
attributes on SSR
Use the component leptos_fluent::SsrHtmlTag
:
#![allow(unused)] fn main() { use leptos::*; use leptos_fluent::{leptos_fluent, SsrHtmlTag}; #[component] pub fn App() -> impl IntoView { leptos_fluent! {{ // ... }}; view! { <SsrHtmlTag/> <LanguageSelector/> } } #[component] fn LanguageSelector() -> impl IntoView { ... } }
How to get values of leptos_fluent!
macro at runtime?
Use provide_meta_context
at the macro initialization and get them
with the method I18n::meta
:
#![allow(unused)] fn main() { let i18n = leptos_fluent! {{ // ... provide_meta_context: true, }}; println!("Macro parameters: {:?}", i18n.meta().unwrap()); }
Different configuration conditional checks
leptos_fluent!
macro does not allows to set the same macro parameter
multiple times with different configuration conditional checks, but
the next approach can be used instead:
#![allow(unused)] fn main() { #[cfg(feature = "ssr")] let param = "ssr-lang"; #[cfg(feature = "hydrate")] let param = "hydrate-lang"; #[cfg(debug_assertions)] let param = "debug-lang"; leptos_fluent! {{ // ... #[cfg(any(feature = "ssr", feature = "hydrate", debug_assertions))] url_param: param, #[cfg(debug_assertions)] initial_language_from_url_param: true, }} }