A crate to help you internationalize your Rocket or Actix Web applications.
It just selects the correct locale for each request, and return the corresponding gettext::Catalog
.
First add it to your Cargo.toml
:
toml
[dependencies]
rocket_i18n = "0.4"
gettext-macros = "0.1" # Provides proc-macros to manage translations
Then, in your main.rs
:
```rust,ignore
use gettextmacros::{compilei18n, includei18n, initi18n};
initi18n!("myweb_app", en, eo, it, pl);
fn main() { rocket::ignite() // Make Rocket manage your translations. .manage(include_i18n!()); // Register routes, etc }
compile_i18n!(); ```
Then in all your requests you'll be able to use the i18n
macro to translate anything.
It takes a gettext::Catalog
and a string to translate as argument.
```rust,ignore use gettextmacros::i18n; use rocketi18n::I18n;
fn route(i18n: I18n) -> &str { i18n!(i18n.catalog, "Hello, world!") } ```
For a detailed explanation about i18n!
, see its documentation.
You can also use the t
macro in your templates, as long as they have a field called catalog
to
store your catalog. This is especially useful for Askama templates.
First, disable the default features so it doesn't pull in all of Rocket.
toml
[dependencies.rocket_i18n]
version = "0.4"
default-features = false
features = ["actix-web"]
Then add it to your application.
```rust use gettextmacros::*; use rocketi18n::{I18n, Internationalized, Translations};
fn route_handler(i18n: I18n) -> &str { i18n!(i18n.catalog, "Hello, world!") }
struct MyState { translations: Translations, }
impl Internationalized for MyState { fn get(&self) -> Translations { self.translations.clone() } }
fn main() { let state = MyState { translations: rocket_i18n::i18n("your-domain", vec![ "en", "fr", "de", "ja" ]); };
App::with_state(state)
.resource("", |r| r.with(route_handler))
.finish();
} ```