woboq / tr

Translation tools for rust
58 stars 10 forks source link

Localisation of rust applications

docs.rs

This repository provides tools for localizing Rust applications, making it easier to translate your software to different languages.

There are two crates

How to translate a rust application

  1. Annotate the strings in your source code with the write macro/functions. You can use

    • The tr! macro from this tr crate (still work in progress), or
    • The gettext function from the gettext or the gettext-rs crate
  2. Run the xtr program over your crate to extract the string in a .pot file

  3. Use the GNU gettext tools to merge, translate, and generate the .mo files

About tr!

Future plans

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) => {
            // Singlular/plural formating
            println!("{}", tr!(
                "The directory {} has one file" | "The directory {} has {n} files" % r.count(),
                folder
            ));
        }
    }
}

About xtr

xtr is a tool that extract translated strings from the source code of a rust crate. The tool is supposed to be compatible with any gettext based functions. But support for the special syntax of the tr! macro has been added.

Usage

xtr src/main.rs -o example.pot

This will extract strings from all the crate's modules and create a file example.pot. You can now use the gettext tools to translate this file.

Differences with xgettext

xtr is basically to be used in place of xgettext for Rust code. xgettext does not currently support the rust language. We can get decent result using the C language, but:

Licence

Contribution

Contributions are welcome. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, should be licensed under the MIT license.

Request for feedback

Please fill your suggestions as issues. Or help by commenting on https://github.com/woboq/tr/issues/1