Crates.io | immortal_intl_rs |
lib.rs | immortal_intl_rs |
version | 0.3.0 |
source | src |
created_at | 2024-04-11 12:39:28.973063 |
updated_at | 2024-06-15 07:22:14.674426 |
description | A simple i18n library for rust |
homepage | |
repository | https://github.com/LyunKi/intl-rs |
max_upload_size | |
id | 1204798 |
size | 13,864 |
A simple i18n library for rust
src/languages
,and you could change it by the env var INTL_RS_RESOURCES
zh_CN
,
and you could change it by the env var INTL_RS_DEFAULT_LANG
Just support the json file for example en_US.json
like below
{
"hello": {
"world": "Hello,World!",
"somebody": "Hello,{{name}}!"
}
}
fn i18n_can_format_messages() {
env::set_var("INTL_RS_RESOURCES", "languages");
let key = "hello.world";
assert_eq!(t!(key), "你好,世界!");
assert_eq!(
t!("unknown key", default:"default message"),
"default message"
);
//default to ensure fallback
//and you can disable it by disable_fallback function
let configs = I18nConfig {
fallback: None,
locale: Some("en".to_owned()),
null_placeholder: None,
args: None,
};
assert_eq!(t!(key, configs: configs), "Hello,World!");
let configs = I18nConfig {
fallback: Some(true),
locale: Some("en_UK".to_owned()),
null_placeholder: None,
args: None,
};
assert_eq!(t!(key, configs: configs), "Hello,World!");
//change the default null placeholder
let configs = I18nConfig {
fallback: Some(true),
locale: Some("en_UK".to_owned()),
null_placeholder: Some("".to_owned()),
args: None,
};
assert_eq!(t!("unknown key", configs: configs), "");
//render template
let mut args: HashMap<&str, &str> = HashMap::new();
args.insert("name", "Donald Trump");
let configs = I18nConfig {
fallback: Some(true),
locale: Some("en_UK".to_owned()),
null_placeholder: Some("".to_owned()),
args: Some(args.clone()),
};
assert_eq!(
t!("hello.somebody", configs: configs),
"Hello,Donald Trump!"
);
assert_eq!(
t!("unknown key",default:"Hey,{{name}}!", args: args.clone()),
"Hey,Donald Trump!"
);
let mut args: HashMap<&str, &str> = HashMap::new();
args.insert("name", "唐纳德·川普");
assert_eq!(
t!("hello.somebody", args: args.clone()),
"你好,唐纳德·川普!"
);
}