Crates.io | languages-rs |
lib.rs | languages-rs |
version | 0.2.0 |
source | src |
created_at | 2021-03-27 01:02:02.29938 |
updated_at | 2021-07-13 02:23:57.989051 |
description | An internationalization library for your projects |
homepage | |
repository | https://github.com/daschdev/languages-rs |
max_upload_size | |
id | 374029 |
size | 33,925 |
An internationalization library for Rust.
Use with JSON language files:
[dependencies]
languages-rs = { version = "0.2.0", features = ["with-json"] }
Use with TOML language files:
[dependencies]
languages-rs = { version = "0.2.0", features = ["with-toml"] }
languages/en.json
{
"hello_world": "Hello world!"
}
src/main.rs
use languages_rs::{Config, Languages, load, Value};
fn main() -> Result<()> {
let mut configuration: Config = Config::default().unwrap();
configuration.add_language("en").unwrap();
// Load all default languages.
let texts: Languages = load(configuration).unwrap();
// Get the English texts from `/languages/en.json`.
let texts_en: LanguagesTexts = texts.try_get_language("en").unwrap();
// Get the `hello_world` text from English texts.
let en_hello_world: Value = texts_en.try_get_text("hello_world").unwrap();
assert!(en_hello_world.is_string());
// Another alternative to get the `hello_world` text from English texts is:
let en_hello_world_2: Value = texts.try_get_text_from_language("en", "hello_world").unwrap();
assert!(en_hello_world_2.is_string());
assert_eq!(en_hello_world, en_hello_world_2);
assert_eq!(en_hello_world.get_string(), en_hello_world_2.get_string());
}
json_files - Languages files with JSON.
$ cargo run --example json_files --features "with-json"
toml_files - Languages files with TOML.
$ cargo run --example toml_files --features "with-toml"
$ cargo test
View the lastest repository changes in the CHANGELOG.md file.
License: MIT
Read file LICENSE for more information.