Easy to use I18n for Rust
Go to file
2019-10-09 05:00:00 +02:00
locales Travis config file 2019-10-09 03:00:40 +02:00
src Config Travis 2019-10-09 05:00:00 +02:00
.gitignore Base files 2019-10-03 10:56:22 +02:00
.travis.yml Config Travis 2019-10-09 05:00:00 +02:00
build.rs Add compile time key check 2019-10-09 02:37:41 +02:00
Cargo.toml Add badges in Cargo.toml 2019-10-09 03:13:34 +02:00
LICENSE-APACHE Base files 2019-10-03 10:56:22 +02:00
LICENSE-MIT Base files 2019-10-03 10:56:22 +02:00
README.md Travis config file 2019-10-09 03:00:40 +02:00

Internationalization

LICENSE Crates.io Version Coverage Status Build Status

An simple compile time i18n implementation in Rust. It throws a compilation error if the translation key is not present, but since the lang argument is dynamic it will panic if the language has not been added for the matching key.

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

Usage

Have a locales/ folder somewhere in your app, root, src, anywhere. with .json files, nested in folders or not. It uses a glob pattern: **/locales/**/*.json to match your translation files.

the files must look like this:

{
  "err.answer.all": {
    "fr": "Échec lors de la récupération des réponses",
    "en": "Failed to retrieve answers"
  },
  "err.answer.delete.failed": {
    "fr": "Échec lors de la suppression de la réponse",
    "en": "Failed to delete answer"
  }
}

Any number of languages can be added, but you should provide them for everything since it will panic if a language is not found when queried for a key.

In your app, jsut call the t! macro

fn main() {
    let lang = "en";
    let res = t!("err.not_allowed", lang);

    assert_eq!("You are not allowed to do this", res);
}

Installation

Internationalization is available on crates.io, include it in your Cargo.toml:

[dependencies]
internationalization = "0.0.2"

Then include it in your code like this:

#[macro_use]
extern crate internationalization;

Or use the macro where you want to use it:

use internationalization::t;

Note

Internationalization will not work if no PWD env var is set at compile time.