Expand description
🔞
r18
is a crate intends to simplify the internationalisation of Rust projects.
Usage
Add r18
as your project dependency:
[dependencies]
r18 = "*"
Create a JSON
translation file whose filename follows
IETF BCP 47
language tag, like below:
// PATH: ./tr/zh-CN.json
{
"Hello, {}": "ä½ å¥½ï¼Œ{}"
}
Then add 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!(); // get locale & set
let name = "ho-229";
println!("{}", r18::tr!("Hello, {}", name));
// reset locale to disable translation
r18::set_locale!("");
assert_eq!("Hello, ho-229", r18::tr!("Hello, {}", name));
}
Macros
Automatically sets the current locale.
Generate translation models and functions
set_locale
and locale
to setup
r18
environment with given translation directory.Returns the current locale.
Sets the current locale.
Translate and content formatting.
Functions
Translate content with the locale setting and given prefix.