internationalization-rs/README.md

91 lines
2.5 KiB
Markdown
Raw Normal View History

2019-10-10 13:23:45 +00:00
# Locales
2019-10-03 11:56:43 +00:00
2019-10-10 13:23:45 +00:00
![LICENSE](https://img.shields.io/crates/l/locales)
[![Crates.io Version](https://img.shields.io/crates/v/locales.svg)](https://crates.io/crates/locales)
2019-10-09 00:37:41 +00:00
[![Coverage Status](https://coveralls.io/repos/github/terry90/internationalization-rs/badge.svg?branch=master)](https://coveralls.io/github/terry90/internationalization-rs?branch=master)
2019-10-09 00:41:34 +00:00
[![Build Status](https://travis-ci.org/terry90/internationalization-rs.svg?branch=master)](https://travis-ci.org/terry90/internationalization-rs)
2019-10-09 00:37:41 +00:00
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.
2019-10-03 11:56:43 +00:00
2019-10-10 13:23:45 +00:00
> API documentation [https://crates.io/crates/locales](https://crates.io/crates/locales)
2019-10-03 11:56:43 +00:00
## Usage
2019-10-09 00:37:41 +00:00
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:
```json
{
2019-10-09 15:34:37 +00:00
"err.user.not_found": {
"fr": "Utilisateur introuvable: $email, $id",
"en": "User not found: $email, $id"
},
2019-10-09 00:37:41 +00:00
"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.
2019-10-09 15:43:15 +00:00
In your app, just call the `t!` macro
2019-10-09 00:37:41 +00:00
2019-10-03 11:56:43 +00:00
```rust
2019-10-10 13:23:45 +00:00
use locales::t;
2019-10-09 15:43:15 +00:00
2019-10-03 11:56:43 +00:00
fn main() {
2019-10-09 00:37:41 +00:00
let lang = "en";
let res = t!("err.not_allowed", lang);
2019-10-03 11:56:43 +00:00
assert_eq!("You are not allowed to do this", res);
}
```
2019-10-09 00:37:41 +00:00
2019-10-09 15:34:37 +00:00
You can use interpolation, any number of argument is OK but you should note that they have to be sorted alphabetically.
To use variables, call the `t!` macro like this:
```rust
2019-10-10 13:23:45 +00:00
use locales::t;
2019-10-09 15:43:15 +00:00
2019-10-09 15:34:37 +00:00
fn main() {
let lang = "en";
let res = t!("err.user.not_found", email: "me@localhost", id: "1", lang);
assert_eq!("User not found: me@localhost, ID: 1", res);
}
```
2019-10-09 00:37:41 +00:00
## Installation
2019-10-10 13:23:45 +00:00
Locales is available on [crates.io](https://crates.io/crates/locales), include it in your `Cargo.toml`:
2019-10-09 00:37:41 +00:00
```toml
[dependencies]
2019-10-10 13:23:45 +00:00
locales = "0.0.3"
2019-10-09 00:37:41 +00:00
```
Then include it in your code like this:
```rust
#[macro_use]
2019-10-10 13:23:45 +00:00
extern crate locales;
2019-10-09 00:37:41 +00:00
```
Or use the macro where you want to use it:
```rust
2019-10-10 13:23:45 +00:00
use locales::t;
2019-10-09 00:37:41 +00:00
```
## Note
2019-10-10 13:23:45 +00:00
Locales will not work if no `PWD` env var is set at compile time.