Crate tr

Source
Expand description

§Internationalisation helper

This crate maily expose a macro that wraps gettext in a convinient ways. See the documentation of the tr! macro.

To translate a rust crate, simply wrap your string within the tr! macro. One can then use the xtr binary to extract all the translatable from a crate in a .po file. GNU gettext tools can be used to process and translate these strings.

The tr! macro also support support rust-like formating.

Example:

#[macro_use]
extern crate tr;
fn main() {
    // use the tr_init macro to tell gettext where to look for translations
    tr_init!("/usr/share/locale/");
    let folder = if let Some(folder) = std::env::args().nth(1) {
        folder
    } else {
        println!("{}", tr!("Please give folder name"));
        return;
    };
    match std::fs::read_dir(&folder) {
        Err(e) => {
            println!("{}", tr!("Could not read directory '{}'\nError: {}",
                                folder, e));
        }
        Ok(r) => {
            // Singular/plural formating
            println!("{}", tr!(
                "The directory {} has one file" | "The directory {} has {n} files" % r.count(),
                folder
            ));
        }
    }
}

§Optional Features

You can change which crate is used as a backend for the translation by setting the features

  • gettext-rs (enabled by default) - This crate wraps the gettext C library
  • gettext - A rust re-implementation of gettext. That crate does not take care of loading the right .mo files, so one must use the [set_translator!] macro with a gettext::Catalog` object

Additionally, this crate permits loading from .po or .mo files directly via the PoTranslator and MoTranslator types, guarded beind the respective mo-translator and po-translator features.

Macros§

set_translator
Set the translator to be used for this crate.
tr
Macro used to translate a string.
tr_initgettext-rs
Initialize the translation for a crate, using gettext’s bindtextdomain
unset_translator
Clears the translator to be used for this crate.

Structs§

MoTranslator(po-translator or mo-translator) and mo-translator
Use this type to load .mo files directly in your application for translations.
PoTranslator(po-translator or mo-translator) and po-translator
Use this type to load .po files directly in your application for translations.

Enums§

MoPoTranslatorLoadErrorpo-translator or mo-translator
This error type is returned when creating a PoTranslator or MoTranslator and an error occurding during parsing.

Traits§

Translator
This trait can be implemented by object that can provide a backend for the translation