3 releases (stable)

Uses old Rust 2015

1.0.1 Sep 3, 2018
1.0.0 Sep 1, 2018
0.1.0 Sep 1, 2018

#31 in #en

Download history 61/week @ 2024-07-28 23/week @ 2024-08-18 58/week @ 2024-08-25 37/week @ 2024-09-01 7/week @ 2024-09-08 13/week @ 2024-09-22 15/week @ 2024-09-29 45/week @ 2024-10-06 33/week @ 2024-10-13 27/week @ 2024-10-20 51/week @ 2024-11-03

113 downloads per month
Used in mindustry-mod-v7

MIT license

12KB
66 lines

r_i18n

An i18n implementation in Rust.

Build Status

API documentation https://crates.io/crates/r_i18n

Table of Contents generated with DocToc

Installation

To install the library, you have to put this line into your Cargo.toml file.

[dependencies]
r_i18n = "version number"

Usage

Configuration

First, create the configuration with the directory that contains your translations files and your languages.

extern crate r_i18n;
use r_i18n::I18nConfig;

fn main() {
    let config: I18nConfig =  I18nConfig{locales: &["en", "fr", "es"], directory: "translations"};
}

Then, load the configuration:

extern crate r_i18n;
use r_i18n::r_i18n;

fn main() {
    let config: I18nConfig =  I18nConfig{locales: &["en", "fr", "es"], directory: "translations"};
    let r_i18n: I18n = I18n::configure(&config);
}

With this example, you will need to have a en.json, fr.json and es.json inside the /translations directory. Each file should looks like that:

{
    "keyword": "value"
}

Example

I have a en.json file that looks like that:

{
    "introduction": "Hello, my name is WebD"
}

Then, in my main.rs

extern crate r_i18n;
use r_i18n::I18n;

fn main() {
    let config: I18nConfig =  I18nConfig{locales: &["en", "fr", "es"], directory: "translations"};
    let r_i18n: I18n = I18n::configure(&config);
    // by default, the current language will be the first element of the locales array. You can do like that if you want to set the language:
    // r_i18n.set_current_lang("fr");
    r_i18n.t("introduction"); // output should be "Hello, my name is WebD"
}

Now, I have a fr.json file that looks like that:

{
    "introduction": "Bonjour, mon nom est WebD"
}

If I set the current language to french:

extern crate r_i18n;
use r_i18n::I18n;

fn main() {
    let config: I18nConfig =  I18nConfig{locales: &["en", "fr", "es"], directory: "translations"};
    let r_i18n: I18n = I18n::configure(&config);
    r_i18n.set_current_lang("fr");
    r_i18n.t("introduction"); // output should be "Bonjour, mon nom est WebD
}

Contribution guide

  1. Fork and Clone the repository
  2. Create your own branch
  3. Start Coding!
  4. Make a pull request when you're done :)

Dependencies

~145KB