#json #i18n #static #rocket #multi-language

json-gettext

A library for getting text from JSON usually for internationalization

34 stable releases

✓ Uses Rust 2018 edition

new 3.1.5 Sep 17, 2019
3.1.2 Jul 28, 2019
2.2.0 May 22, 2019
2.1.0 Nov 17, 2018
1.7.0 Oct 15, 2018

#15 in Internationalization (i18n)

Download history 73/week @ 2019-06-03 72/week @ 2019-06-10 110/week @ 2019-06-17 200/week @ 2019-06-24 181/week @ 2019-07-01 87/week @ 2019-07-08 13/week @ 2019-07-15 59/week @ 2019-07-22 121/week @ 2019-07-29 121/week @ 2019-08-05 25/week @ 2019-08-12 5/week @ 2019-08-19 332/week @ 2019-08-26 164/week @ 2019-09-02 47/week @ 2019-09-09

493 downloads per month
Used in 4 crates (3 directly)

MIT license

47KB
1K SLoC

JSON Get Text

Build Status

This is a library for getting text from JSON usually for internationalization.

Example

#[macro_use] extern crate json_gettext;

let ctx = static_json_gettext_build!(
    "en_US",
    "en_US", "langs/en_US.json",
    "zh_TW", "langs/zh_TW.json"
).unwrap();

assert_eq!("Hello, world!", get_text!(ctx, "hello").unwrap());
assert_eq!("哈囉,世界!", get_text!(ctx, "zh_TW", "hello").unwrap());

Rocket Support

This crate supports the Rocket framework. In order to reload changed json files instead of recompiling the program you have to enable the rocketly feature for this crate.

[dependencies.json-gettext]
version = "*"
features = ["rocketly"]

Then, use the static_json_gettext_build_rocketly macro instead of the static_json_gettext_build macro to build a JSONGetText(JSONGetTextManager).

#[macro_use] extern crate json_gettext;

#[macro_use] extern crate rocket;

use rocket::State;
use rocket::response::Redirect;

use json_gettext::JSONGetTextManager;

#[get("/")]
fn index(ctx: State<JSONGetTextManager>) -> Redirect {
    Redirect::temporary(uri!(hello: lang = ctx.get_default_key()))
}

#[get("/<lang>")]
fn hello(ctx: State<JSONGetTextManager>, lang: String) -> String {
    format!("Ron: {}", get_text!(ctx, lang, "hello").unwrap().as_str().unwrap())
}

fn main() {
    rocket::ignite()
        .attach(JSONGetTextManager::fairing(|| {
            static_json_gettext_build_rocketly!("en_US",
                "en_US", "langs/en_US.json",
                "zh_TW", "langs/zh_TW.json"
            )
        }))
        .mount("/", routes![index, hello])
        .launch();
}

If you are not using the release profile, JSONGetTextManager can reload the json files automatically if needed.

Crates.io

https://crates.io/crates/json-gettext

Documentation

https://docs.rs/json-gettext

License

MIT

Dependencies

~1.7–3MB
~74K SLoC