Internationalization
v0.7.0 brings first-class internationalization to Tidos. Powered by Mozilla's Fluent localization system, you can now translate any string in any component with a single macro call.
Enabling i18n
Add the i18n feature flag to your Cargo.toml:
1[dependencies]2tidos = { version = "0.7.0", features = ["rocket", "i18n"] }
Create a Tidos.toml in your project root to configure the locale directory and default language:
1[default]2resource_location = "translations"3default_locale = "en-US"4resources = ["common.ftl"]
Then call enable_i18n! once at the top of main.rs to load the translation files at startup:
1use tidos::i18n::enable_i18n;23enable_i18n!();45#[rocket::main]6async fn main() { /* ... */ }
Translating strings with i18n!
Inside any component, import and call i18n! with a Fluent message key:
1use tidos::i18n::i18n;23impl Component for Greeting {4 fn to_render(&self, page: &mut Page) {5 view! {6 <h1>{i18n!("greeting")}</h1>7 }8 }9}
Variables are passed as alternating key-value pairs after the message key:
1// Fluent message with variable:2// welcome = Welcome back, { $name }!34view! {5 <p>{i18n!("welcome", "name", &self.username)}</p>6}
Translation files
Fluent .ftl files live under your resource_location, organised by locale:
1translations/2 en-US/3 common.ftl4 nl-NL/5 common.ftl
Fluent supports plural forms, gender variants, and other selectors out of the box, so you can handle complex grammatical rules without hand-written conditionals:
1new-messages =2 { $count ->3 [one] You have one new message.4 *[other] You have { $count } new messages.5 }
Locale routing with Lang
Add a Lang parameter to your route to accept a locale prefix in the URL. Tidos picks up the active locale automatically inside page!:
1use tidos::i18n::Lang;23#[get("/<lang>/dashboard")]4pub fn dashboard(lang: Lang) -> Page {5 page! {6 <main>7 <h1>{i18n!("dashboard-title")}</h1>8 </main>9 }10}
The lang parameter doubles as a Rocket request guard and as the active locale context for all i18n! calls on that page.
Upgrading
No breaking changes for existing Tidos users. The i18n feature is entirely opt-in:
1[dependencies]2tidos = "0.7.0" # existing usage unaffected