An i18n implementation in Rust.
API documentation https://crates.io/crates/r_i18n
Table of Contents generated with DocToc
To install the library, you have to put this line into your Cargo.toml file.
toml
[dependencies]
r_i18n = "version number"
First, create the configuration with the directory that contains your translations files and your languages. ```rust extern crate ri18n; use ri18n::I18nConfig;
fn main() {
let config: I18nConfig = I18nConfig{locales: &["en", "fr", "es"], directory: "translations"};
}
Then, load the configuration:
rust
extern crate ri18n;
use ri18n::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:
json
{
"keyword": "value"
}
```
I have a en.json file that looks like that:
json
{
"introduction": "Hello, my name is WebD"
}
Then, in my main.rs
```rust extern crate ri18n; use ri18n::I18n;
fn main() { let config: I18nConfig = I18nConfig{locales: &["en", "fr", "es"], directory: "translations"}; let ri18n: 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: // ri18n.setcurrentlang("fr"); r_i18n.t("introduction"); // output should be "Hello, my name is WebD" } ```
Now, I have a fr.json file that looks like that:
json
{
"introduction": "Bonjour, mon nom est WebD"
}
If I set the current language to french: ```rust extern crate ri18n; use ri18n::I18n;
fn main() { let config: I18nConfig = I18nConfig{locales: &["en", "fr", "es"], directory: "translations"}; let ri18n: I18n = I18n::configure(&config); ri18n.setcurrentlang("fr"); r_i18n.t("introduction"); // output should be "Bonjour, mon nom est WebD } ```