r18 0.2.2

A simply I18n crate for Rust.
Documentation

🔞

crate.io docs build

r18 is a crate for internationalising Rust projects.

Usage

Add r18 as your project dependency.

[dependencies]
r18 = "0.1"

Create a JSON translation file with name BCP 47 language tag as naming format, like below:

// PATH: ./tr/zh-CN.json
{
    "Hello, {}": "你好,{}"
}

Then add r18::init! to the global scope of your code with the directory where translation files in (in following example is ./tr).

r18::init!("tr");

After initialising the r18, use auto_detect! to detect locale and load translation model automatically.
If you want, you can use set_locale! to set locale manually.
After above process, use tr! to get your text which has been translated.

r18::init!("tr");

fn main() {
    r18::auto_detect!();

    let name = "ho-229";
    println!("{}", tr!("Hello, {}", name));

    // reset locale to disable translation
    r18::set_locale!("");
    assert_eq!("Hello, ho-229", tr!("Hello, {}", name));
}

You can find a complete example here. You can run the example with following command:

cargo run -p example

Credit