Update README.md

pull/10/head
Baptiste Gelez 6 years ago
parent b79aa0776b
commit 7fa1b0bd6b

@ -4,11 +4,9 @@ A crate to help you internationalize your Rocket applications.
## Features
- Create `.po` files for locales listed in `po/LINGUAS`, from a POT file
- Update `.po` files from the POT file if needed
- Compile `.po` files into `.mo` ones
- Build helpers (with the `build` feature enabled), to update and compile PO files.
- Select the correct locale for each request
- Integrates with Tera templates
- Provides a macro to internationalize any string.
## Usage
@ -17,66 +15,58 @@ First add it to your `Cargo.toml` (you have to use the git version, because we c
```toml
[dependencies.rocket_i18n]
git = "https://github.com/BaptisteGelez/rocket_i18n"
rev = "457b88c59ec31905a9193df43df58bee55b4b83d"
rev = "<LATEST COMMIT>"
```
Then, in your `main.rs`:
```rust
extern crate rocket;
#[macro_use]
extern crate rocket_i18n;
// ...
fn main() {
rocket::ignite()
// Register the fairing. The parameter is the domain you want to use (the name of your app most of the time)
.attach(rocket_i18n::I18n::new("my_app"))
// Eventually register the Tera filters (only works with the master branch of Rocket)
.attach(rocket_contrib::Template::custom(|engines| {
rocket_i18n::tera(&mut engines.tera);
}))
// Make Rocket manage your translations.
.manage(rocket_i18n::i18n(vec![ "en", "fr", "de", "ja" ]));
// Register routes, etc
}
```
### Using Tera filters
Then in all your requests you'll be able to use the `i18n` macro to translate anything.
It takes a `gettext::Catalog` and a string to translate as argument.
If you called `rocket_i18n::tera`, you'll be able to use two Tera filters to translate your interface.
```rust
# #[macro_use] extern crate rocket_i18n;
The first one, `_`, corresponds to the `gettext` function of gettext. It takes a string as input and translate it. Any argument given to the filter can
be used in the translated string using the Tera syntax.
use rocket_i18n::I18n;
```jinja
<p>{{ "Hello, world" | _ }}</p>
<p>{{ "Your name is {{ name }}" | _(name=user.name) }}
#[get("/")]
fn route(i18n: I18n) -> &str {
i18n!(i18n.catalog, "Hello, world!")
}
```
The second one, `_n`, is equivalent to `ngettext`. It takes the plural form as input, and two required arguments in addition to those you may want to use for interpolation:
- `singular`, the singular form of this string
- `count`, the number of items, to determine how the string should be pluralized
For strings that may have a plural form, just add the plural and the number of element to the
arguments
```jinja
<p>{{ "{{ count }} new messages" | _n(singular="One new message", count=messages.unread_count) }}</p>
```rust
i18n!(i18n.catalog, "One new message", "{0} new messages", 42);
```
### In Rust code
You can also use all the gettext functions in your Rust code.
Any extra argument, after a `;`, will be used for formatting.
```rust
use rocket_i18n;
let user_name = "Alex";
i18n!(i18n.catalog, "Hello {0}!"; user_name);
```
#[get("/")]
fn index() -> String {
gettext("Hello, world!")
}
When using it with plural, `{0}` will be the number of elements, and other arguments will start
at `{1}`.
#[get("/<name>")]
fn hello(name: String) -> String {
format!(gettext("Hello, {}!"), name)
}
```
Because of its design, `rocket_i18n` is only compatible with askama. You can use
the `t` macro in your templates, as long as they have a field called `catalog` to
store your catalog.
### Editing the POT

Loading…
Cancel
Save