[][src]Macro tr::tr

macro_rules! tr {
    ($msgid:tt, $($tail:tt)* ) => { ... };
    ($msgid:tt) => { ... };
    ($msgctx:tt => $msgid:tt, $($tail:tt)* ) => { ... };
    ($msgctx:tt => $msgid:tt) => { ... };
    ($msgid:tt | $plur:tt % $n:expr, $($tail:tt)* ) => { ... };
    ($msgid:tt | $plur:tt % $n:expr) => { ... };
    ($msgctx:tt => $msgid:tt | $plur:tt % $n:expr, $($tail:tt)* ) => { ... };
    ($msgctx:tt => $msgid:tt | $plur:tt % $n:expr) => { ... };
}

Macro used to translate a string.

// Prints "Hello world!", or a translated version depending on the locale
println!("{}", tr!("Hello world!"));

The string to translate need to be a string literal, as it has to be extracted by the xtr tool. One can add more argument following a subset of rust formating

let name = "Olivier";
// Prints "Hello, Olivier!",  or a translated version of that.
println!("{}", tr!("Hello, {}!", name));

Plural are using the "singular" | "plural" % count syntax. {n} will be replaced by the count.

let number_of_items = 42;
println!("{}", tr!("There is one item" | "There are {n} items" % number_of_items));

Normal formating rules can also be used:

let number_of_items = 42;
let folder_name = "/tmp";
println!("{}", tr!("There is one item in folder {}"
       | "There are {n} items in folder {}" % number_of_items, folder_name));

If the same string appears several time in the crate, it is necessary to add a disambiguation context, using the "context" => syntax:

// These two strings are both "Open" in english, but they may be different in a
// foreign language. Hence, a context string is necessary.
let action_name = tr!("File Menu" => "Open");
let state = tr!("Document State" => "Open");

To enable the translation, one must first call the tr_init! macro once in the crate. To translate the strings, one can use the xtr utility to extract the string, and use the other GNU gettext tools to translate them.