1 个不稳定版本
使用旧的 Rust 2015
0.1.0 | 2017 年 7 月 16 日 |
---|
在 国际化(i18n) 中排名第 223
9KB
117 代码行
litelocale
🇪🇸 轻量级 Rust 国际化库。
# Add to your Cargo.toml file dependencies:
litelocale = "0.1.0"
# or: litelocale = { git = "https://github.com/stpettersens/litelocale.git" }
您可以使用 litelocale
为您的 Rust 程序提供简单的国际化。
以下是一个简单的程序,可以根据选定的区域显示从 1 到 5 的数字,英文或西班牙语。
extern crate litelocale;
use litelocale::*;
fn main() {
// Declare a vector of numbers in English.
let nums = vec!["one", "two", "three", "four", "five"];
// Declare a new locale, Spanish (Español, es).
let mut es = Locale::new();
// Select es as the locale.
let sel = "es";
// Load Spanish (es) locale when English (en) is not selected.
if sel != "en" {
// Add each number to locale.
// English-Spanish-Phonetic Spanish (phonetics are optional).
es.add_message(LocaleMessage::new("one", "uno", "oono"));
es.add_message(LocaleMessage::new("two", "dos", "dos"));
es.add_message(LocaleMessage::new("three", "tres", "trez"));
es.add_message(LocaleMessage::new("four", "cuatro", "katro"));
es.add_message(LocaleMessage::new("five", "cinco", "finco"));
/* It is recommended in more substantial programs that you instead
deserialize LocaleMessages from an external structure.
E.g. from JSON using serde_json:
let mut locale = Locale::new();
if sel != "en {
let file = File::open("my_locale.json");
for line in BufReader::new(file).lines() {
locale.add_message(
serde_json::from_str(&line.unwrap())
.unwrap());
// <- {"locstr":"one","message":"uno","phonetic":"oono"}
// , etc.
}
}
*/
}
// Print each number in Spanish as sel == "es".
// localize returns the localization for each English word.
for n in nums {
println!("-> {}", localize(n, &es)); // -> uno, -> dos, -> tres, etc.
// If sel == "en"; output is as-is in original vector:
// -> one, -> two, -> three, etc.
}
}
依赖项
~0.4–1MB
~23K SLoC