[][src]Crate rocket_i18n

Rocket I18N

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:

rocket_i18n = "0.4"
gettext-macros = "0.1" # Provides proc-macros to manage translations

Then, in your main.rs:

This example is not tested
use gettext_macros::{compile_i18n, include_i18n, init_i18n};

init_i18n!("my_web_app", en, eo, it, pl);

fn main() {
        // Make Rocket manage your translations.
        // Register routes, etc


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.

This example is not tested
use gettext_macros::i18n;
use rocket_i18n::I18n;

fn route(i18n: I18n) -> &str {
    i18n!(i18n.catalog, "Hello, world!")

For strings that may have a plural form, just add the plural and the number of element to the arguments

This example is not tested
i18n!(i18n.catalog, "One new message", "{0} new messages", 42);

Any extra argument, after a ;, will be used for formatting.

This example is not tested
let user_name = "Alex";
i18n!(i18n.catalog, "Hello {0}!"; user_name);

When using it with plural, {0} will be the number of elements, and other arguments will start at {1}.

Because of its design, rocket_i18n is only compatible with askama, ructe or compiled templates in general. You can use the t macro in your templates, as long as they have a field called catalog to store your catalog.



Works the same way as gettext_macros::i18n, but without needing to give a gettext::Catalog as first argument.



Catalog represents a set of translation strings parsed out of one MO file.


A request guard to get the right translation catalog for the current request.


ParseOptions allows setting options for parsing MO catalogs.



Represents an error encountered while parsing an MO file.



Loads translations at runtime. Usually used with rocket::Rocket::manage.

Type Definitions