Internationalization

Tidos provides built-in i18n support via Fluent. Enable it with the i18n feature flag to get locale-aware translations, pluralization, and gender variants.

1. Enable the Feature

1[dependencies]
2tidos = { version = "0.8.0-rc.1", features = ["rocket", "i18n"] }

2. Create Tidos.toml

Place Tidos.toml in your project root to configure translation file locations:

1[default]
2resource_location = "translations"
3default_locale = "en-US"
4resources = ["common.ftl"]

3. Add Translation Files

Organize .ftl files by locale under the translations directory:

1translations/
2├── en-US/
3│ └── common.ftl
4└── nl-NL/
5 └── common.ftl
1# translations/en-US/common.ftl
2greeting = Hello, { $name }!
3
4welcome-message =
5 Welcome back, { $name }.
6 You have { $count ->
7 [one] one new message
8 *[other] { $count } new messages
9 }.

4. Initialize in main.rs

Call enable_i18n! once at startup to load the translation configuration:

1// main.rs
2use tidos::i18n::enable_i18n;
3
4enable_i18n!();
5
6#[rocket::main]
7async fn main() {
8 rocket::build()
9 .mount("/", routes![index])
10 .launch()
11 .await
12 .unwrap();
13}

5. Add Lang to Routes

The locale is expected as the first URL path segment. Add lang: Lang to your route handler and page! picks it up automatically:

1use tidos::i18n::Lang;
2use tidos::{page, Page};
3use rocket::get;
4
5// The locale is the first path segment: /en-US or /nl-NL
6#[get("/<lang>")]
7pub fn index(lang: Lang) -> Page {
8 page! {
9 <main>
10 <Greeting />
11 </main>
12 }
13}

6. Translate with i18n!

Call i18n! inside any component with a Fluent message key and optional variable pairs:

1use tidos::{view, Component, Page};
2use tidos::i18n::i18n;
3
4pub struct Greeting;
5
6impl Component for Greeting {
7 fn to_render(&self, page: &mut Page) {
8 view! {
9 <section>
10 // Simple lookup
11 <h1>{i18n!("greeting", ("name", "Alice"))}</h1>
12
13 // Lookup with plural variable
14 <p>{i18n!("welcome-message", ("name", "Alice"), ("count", 3))}</p>
15 </section>
16 }
17 }
18}