r18 0.2.0

A simply I18n crate for Rust.
Documentation
r18-0.2.0 has been yanked.

🔞

crate.io docs build

r18 is a crate for internationalized Rust projects.

Usage

Add r18 to your project's dependencies.

[dependencies]
r18 = "0.1"

Create a JSON translation file whose name format is BCP 47 language tag in a directory and write it as follows:

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

Then add r18::init! to the global area of your code with the translation file directory path (is ./tr in this example) relative to your project root.

r18::init!("tr");

After initialization, use auto_detect! to detect locale and load translation model (optional, you can use set_locale! to set locale manually), then use tr! to translate 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));
}

For a complete example, you can see here. you can run the example with:

cargo run -p example

Alternative