2 个稳定版本
1.0.1 | 2022年8月5日 |
---|
#215 in 国际化 (i18n)
23KB
359 行
message-locator
消息资产的本地化。此库使用 language-objects 来处理语言数据。
用法
将此添加到 Cargo.toml
[dependencies]
message-locator = "1"
maplit = "1.0"
tokio = { version = "1", features = ["full"] }
示例资产位于 res/lang/en/_.json
{
"message_id": "Some message",
"parameterized": "Here: $x",
"contextual_male": "Male message",
"contextual_female": "Female message",
"contextual_other": "Other message",
"qty_empty": "Empty ($number)",
"qty_one": "One ($number)",
"qty_multiple": "Multiple ($number)"
}
示例代码
use message_locator::{
MessageLocator, MessageLocatorOptions, MessageLocatorAssetOptions,
MessageLocatorLoadMethod,
locator_vars,
};
use maplit::hashmap;
#[tokio::main]
async fn main() {
let mut msg_locator = MessageLocator::new(
MessageLocatorOptions::new()
// Specify supported locale codes.
// The form in which the locale code appears here
// is a post-component for the assets "src" path.
// For example: "path/to/res/lang/en-US"
.supported_locales(vec!["en", "en-US", "pt-BR"])
.default_locale("en-US")
.fallbacks(hashmap! {
"en-US" => vec!["en"],
"pt-BR" => vec!["en-US"],
})
.assets(MessageLocatorAssetOptions::new()
.src("res/lang")
.base_file_names(vec!["_"])
// "clean_unused" indicates whether to clean previous unused locale data.
.clean_unused(true)
// Specify MessageLocatorLoadMethod::FileSystem or MessageLocatorLoadMethod::Http
.load_method(MessageLocatorLoadMethod::FileSystem))
); // msg_locator
if !msg_locator.load(None).await {
// failed to load
return;
}
println!("{}", msg_locator.get("_.message_id"));
println!("{}", msg_locator.get_formatted("_.parameterized", vec![ &locator_vars!{
"x" => "foo"
} ]));
println!("{}", msg_locator.get_formatted("_.contextual", vec![ &"female" ]));
}
依赖项
~8–22MB
~334K SLoC