Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stpettersens/litelocale
:es: Lightweight localization library for Rust.
https://github.com/stpettersens/litelocale
Last synced: about 1 month ago
JSON representation
:es: Lightweight localization library for Rust.
- Host: GitHub
- URL: https://github.com/stpettersens/litelocale
- Owner: stpettersens
- License: other
- Created: 2017-07-08T19:16:09.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-07-17T17:50:00.000Z (over 7 years ago)
- Last Synced: 2024-10-14T01:46:50.605Z (3 months ago)
- Language: Rust
- Homepage: https://docs.rs/litelocale
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### litelocale
>:es: Lightweight localization library for Rust.[![Build Status](https://travis-ci.org/stpettersens/litelocale.png?branch=master)](https://travis-ci.org/stpettersens/litelocale)
[![Build status](https://ci.appveyor.com/api/projects/status/x937wesh830othcx?svg=true)](https://ci.appveyor.com/project/stpettersens/litelocale)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/stpettersens/litelocale/blob/master/LICENSE)
[![Crates.io](https://img.shields.io/crates/v/litelocale.svg)](https://crates.io/crates/litelocale)```toml
# 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:
```rust
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.
}
}
```[Documentation](https://docs.rs/litelocale)