Skip to content

i18n ​

Locales, catalogs, optional path prefix and cookie

Cratesova-i18n 0.1.2
Plugin idi18n
CategoryContent

Install ​

bash
cargo add sova --features i18n

Features ​

FeatureWhat you get
i18nLocales, catalogs, optional path prefix.
i18n-cookieRemember locale in a cookie.

Overview ​

When: locales, catalogs, optional path prefix / cookie.

Does:

  • Locale negotiation
  • t!(…) / catalog lookups
  • Path prefix + cookie features

Example ​

rust
app.install(I18n::new().locales(["en", "ru"]).default("en"));

Config ​

toml
[i18n]
default = "en"
cookie = "locale"    # needs feature i18n-cookie + CookieLayer
watch = true         # hot-reload catalogs when feature allows

Quick start ​

Add locales on the web preset (templates already available). Cookie locale needs feature i18n-cookie (Cookies come from session on the web preset):

rust
use sova::prelude::*;
use sova::{I18n, Locale, Parser, ServerArgs};

#[tokio::main]
async fn main() -> Result<()> {
    let args = ServerArgs::parse();
    args.init_tracing();

    let mut app = App::web()
        .site("App")
        .public_url("https://example.com")
        .into_app();

    app.install(
        I18n::new(
            "locales",
            vec![
                Locale::new("en").with_name("English"),
                Locale::new("ru").with_name("Русский"),
            ],
        )
        .fallback("en")
        .cookie("locale")
        .set_locale_cookie(true),
    );

    app.run().await
}

In MiniJinja register the helper with sova_templates::register_per_request(app, "t", sova::template_fn) (or build Templates::minijinja(…).per_request(…) yourself). Demos: i18n, templates_i18n.

toml
[i18n]
default = "en"
cookie = "locale"
# watch = true

Examples ​

templates · vld · meta