[][src]Crate json_gettext

JSON Get Text

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).

This example is not tested
#[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.

Re-exports

pub extern crate regex;
pub extern crate serde_json;

Macros

get_text

Used for getting single or multiple text from context.

static_json_gettext_build

Used for including json files into your executable binary file for building a JSONGetText instance.

Structs

JSONGetText

A wrapper for context and a default key. Keys are usually considered as locales.

JSONGetTextBuilder

To build a JSONGetText instance, this struct can help you do that step by step.

Enums

JSONGetTextBuildError
JSONGetTextValue

Represents any valid JSON value. Reference can also be wrapped.

Value

Represents any valid JSON value.

Type Definitions

Context