Crate languages_rs[][src]

Expand description

languages-rs

An internationalization library for your applications.

Features

  • JSON or TOML languages files.
  • Only can use Objects, Arrays and Strings.
  • Customize the languages directory.

JSON Language File

{
    "hello_world": "Hello, world!",
    "home": {
        "title": "Home page",
        "description": "This is the home page."
    },
    "data": {
        "messages": [
            "Message 1",
            "Message 2"
        ]
    ]
}

TOML Language File

hello_world = "Hello, world!"

[home]
title = "Home page"
description = "This is the home page."

[data]
messages = [
    "Message 1",
    "Message 2"
]

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 mut texts: Languages = load(configuration).unwrap();

    // Get the English texts from `/languages/es.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());

    // Other alternative to get the `hello_world` text from English texts.
    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());
}

Structs

Enums

Functions

Load the languages of a configuration and return the Languages struct.