A crate to help you internationalize your Rocket applications.
Find a file
2018-11-20 18:59:23 +01:00
src Fix some issues with macros 2018-11-20 18:59:09 +01:00
.gitignore Initial commit 2018-06-17 12:50:35 +01:00
.travis.yml Add travis.yml 2018-09-15 13:28:18 +02:00
Cargo.toml Update dependencies 2018-11-20 18:59:23 +01:00
LICENSE Add metadata + document the crate 2018-06-17 14:46:46 +01:00
README.md Update README.md 2018-10-25 22:22:22 +01:00
rust-toolchain Wait for process to finish successfully 2018-11-01 19:25:50 +01:00

Rocket I18N Build Status

A crate to help you internationalize your Rocket applications.

Features

  • Build helpers (with the build feature enabled), to update and compile PO files.
  • Select the correct locale for each request
  • Provides a macro to internationalize any string.

Usage

First add it to your Cargo.toml (you have to use the git version, because we can't publish the latest version on https://crates.io as it depends on the master branch of Rocket):

[dependencies.rocket_i18n]
git = "https://github.com/BaptisteGelez/rocket_i18n"
rev = "<LATEST COMMIT>"

Then, in your main.rs:

extern crate rocket;
#[macro_use]
extern crate rocket_i18n;

fn main() {
    rocket::ignite()
        // Make Rocket manage your translations.
        .manage(rocket_i18n::i18n(vec![ "en", "fr", "de", "ja" ]));
        // 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.

# #[macro_use] extern crate rocket_i18n;

use rocket_i18n::I18n;

#[get("/")]
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

i18n!(i18n.catalog, "One new message", "{0} new messages", 42);

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

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. You can use the t macro in your templates, as long as they have a field called catalog to store your catalog.

Editing the POT

For those strings to be translatable you should also add them to the po/YOUR_DOMAIN.pot file. To add a simple message, just do:

msgid "Hello, world" # The string you used with your filter
msgstr "" # Always empty

For plural forms, the syntax is a bit different:

msgid "You have one new notification" # The singular form
msgid_plural "You have {{ count }} new notifications" # The plural one
msgstr[0] ""
msgstr[1] ""