Code Monkey home page Code Monkey logo

rocket_i18n's Introduction

Rocket I18N Build Status

A crate to help you internationalize your Rocket or Actix Web applications.

It just selects the correct locale for each request, and return the corresponding gettext::Catalog.

Usage

First add it to your Cargo.toml:

[dependencies]
rocket_i18n = "0.4"
gettext-macros = "0.1" # Provides proc-macros to manage translations

Then, in your main.rs:

# use rocket;
use gettext_macros::{compile_i18n, include_i18n, init_i18n};

init_i18n!("my_web_app", en, eo, it, pl);

fn main() {
    rocket::ignite()
        // Make Rocket manage your translations.
        .manage(include_i18n!());
        // Register routes, etc
}

compile_i18n!();

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.

use gettext_macros::i18n;
use rocket_i18n::I18n;

#[get("/")]
fn route(i18n: I18n) -> &str {
    i18n!(i18n.catalog, "Hello, world!")
}

For strings that may have a plural form, just add the plural and the number of element to the arguments

i18n!(i18n.catalog, "One new message", "{0} new messages", 42);

Any extra argument, after a ;, will be used for formatting.

let user_name = "Alex";
i18n!(i18n.catalog, "Hello {0}!"; user_name);

When using it with plural, {0} will be the number of elements, and other arguments will start at {1}.

Because of its design, rocket_i18n is only compatible with askama, ructe or compiled templates in general. You can use the t macro in your templates, as long as they have a field called catalog to store your catalog.

Using with Actix Web

First, disable the default features so it doesn't pull in all of Rocket.

[dependencies.rocket_i18n]
version = "0.4"
default-features = false
features = ["actix-web"]

Then add it to your application.

use gettext_macros::*;
use rocket_i18n::{I18n, Internationalized, Translations};

fn route_handler(i18n: I18n) -> &str {
    i18n!(i18n.catalog, "Hello, world!")
}

#[derive(Clone)]
struct MyState {
    translations: Translations,
}

impl Internationalized for MyState {
    fn get(&self) -> Translations {
        self.translations.clone()
    }
}

fn main() {
    let state = MyState {
        translations: rocket_i18n::i18n("your-domain", vec![ "en", "fr", "de", "ja" ]);
    };

    App::with_state(state)
        .resource("", |r| r.with(route_handler))
        .finish();
}

rocket_i18n's People

Contributors

asonix avatar elegaanz avatar lthms avatar ogoffart avatar trinity-1686a avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rocket_i18n's Issues

Correctly parse local from client

Currently client's local is determined by the first local before a comma, but local may contain not only language but also country and some sort of priority tag. Those should get stripped before calling set_var. For exemple when connecting to a Plume instance, as my exact local is fr-FR,en-US;q=0.7,en;q=0.3, set_var is called on fr-FR, but translation is only available for fr, so I get default language instead of french.
How to reproduce :

$ curl https://baptiste.gelez.xyz/ -H "Accept-Language: fr" | grep h1
$ curl https://baptiste.gelez.xyz/ -H "Accept-Language: fr-FR" | grep h1

Notice one is in french, the other in english

Request may be translated to the wrong language due to race condition

There is a race condition due to use of a global variable to store language, if two request are made close enought, one will inherit the language of the other
How to reproduce :

$ for i in `seq 1 50`; do curl https://baptiste.gelez.xyz/ -H "Accept-Language: en" 2>/dev/null  | grep 'h1'& done
$ for i in `seq 1 50`; do curl https://baptiste.gelez.xyz/ -H "Accept-Language: fr" 2>/dev/null  | grep 'h1'& done

Running one command then another give consistant results, but running both at the same time on different terminals sometime give inconsistant language result.
I don't know how this can be easily fixed as gettext-rs make heavy use of global state to store local

rocket 0.5 compatibility

Hey folks,

as always, thanks for your work on this lib!

Rocket recently released a 0.5 release candidate, with Rust stable and async support. I'm wondering, are there plans to update this library to the new version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.