2 unstable releases

0.2.0 Jul 13, 2021
0.1.0 Mar 27, 2021

#142 in Internationalization (i18n)

28 downloads per month
Used in phanes

MIT license

31KB
305 lines

languages-rs   Crates.io version CI workflow status

An internationalization library for Rust.

Installation

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"] }

Basic Usage

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());
}

Examples

  • 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"
    

Testing

$ cargo test

Authors

Changelog

View the lastest repository changes in the CHANGELOG.md file.

License: MIT

Read file LICENSE for more information.

Dependencies

~0.5–1.3MB
~29K SLoC