1 unstable release
Uses old Rust 2015
0.1.0 | Jul 16, 2017 |
---|
#256 in Internationalization (i18n)
9KB
117 lines
litelocale
🇪🇸 Lightweight localization library for Rust.
# Add to your Cargo.toml file dependencies:
litelocale = "0.1.0"
# or: litelocale = { git = "https://github.com/stpettersens/litelocale.git" }
You can use litelocale
to provide simple localization for your Rust program.
Below is a simple program that can display numbers from 1 to 5 in either English or Spanish depending on the selected locale:
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.
}
}
Dependencies
~0.3–1MB
~21K SLoC