json_gettext/
lib.rs

1/*!
2# JSON Get Text
3
4This is a library for getting text from JSON usually for internationalization.
5
6## Example
7
8```rust,ignore
9#[macro_use] extern crate json_gettext;
10
11let ctx = static_json_gettext_build!(
12    "en_US";
13    "en_US" => "langs/en_US.json",
14    "zh_TW" => "langs/zh_TW.json"
15).unwrap();
16
17assert_eq!("Hello, world!", get_text!(ctx, "hello").unwrap());
18assert_eq!("哈囉,世界!", get_text!(ctx, "zh_TW", "hello").unwrap());
19```
20
21## Rocket Support
22
23This crate supports the Rocket framework. In order to reload changed json files instead of recompiling the program you have to enable the `rocket` feature for this crate.
24
25```toml
26[dependencies.json-gettext]
27version = "*"
28features = ["rocket"]
29```
30
31Then, use the `static_json_gettext_build_for_rocket` macro instead of the `static_json_gettext_build` macro to build a `JSONGetText`(`JSONGetTextManager`).
32
33```rust,ignore
34#[macro_use] extern crate json_gettext;
35
36#[macro_use] extern crate rocket;
37
38use rocket::State;
39use rocket::response::Redirect;
40
41use json_gettext::JSONGetTextManager;
42
43#[get("/")]
44fn index(ctx: &State<JSONGetTextManager>) -> Redirect {
45    Redirect::temporary(uri!(hello(lang = ctx.get_default_key())))
46}
47
48#[get("/<lang>")]
49fn hello(ctx: &State<JSONGetTextManager>, lang: String) -> String {
50    format!("Ron: {}", get_text!(ctx, lang, "hello").unwrap().as_str().unwrap())
51}
52
53#[launch]
54fn rocket() -> _ {
55    rocket::build()
56        .attach(static_json_gettext_build_for_rocket!(
57            "en_US";
58            "en_US" => "langs/en_US.json",
59            "zh_TW" => "langs/zh_TW.json"
60        ))
61        .mount("/", routes![index, hello])
62}
63```
64
65If you are not using the `release` profile, `JSONGetTextManager` can reload the json files automatically if needed.
66
67## `unic-langid` Support
68
69Since string comparison could be slow, the `language_region_pair` feature, the `language` feature or the `region` feature can be enabled to change key's type to `(Language, Option<Region>)`, `Language` or `Region` respectively where `Language` and `Region` structs are in the `unic-langid` crate.
70
71In this case, the `key!` macro would be useful for generating a `Key` instance from a literal string.
72
73For example,
74
75```toml
76[dependencies.json-gettext]
77version = "*"
78features = ["language_region_pair", "rocket"]
79```
80
81```rust,ignore
82#[macro_use]
83extern crate rocket;
84
85#[macro_use]
86extern crate rocket_accept_language;
87
88#[macro_use]
89extern crate json_gettext;
90
91use rocket::State;
92
93use rocket_accept_language::unic_langid::subtags::Language;
94use rocket_accept_language::AcceptLanguage;
95
96use json_gettext::{JSONGetTextManager, Key};
97
98const LANGUAGE_EN: Language = language!("en");
99
100#[get("/")]
101fn index(ctx: &State<JSONGetTextManager>, accept_language: &AcceptLanguage) -> String {
102    let (language, region) = accept_language.get_first_language_region().unwrap_or((LANGUAGE_EN, None));
103
104    format!("Ron: {}", get_text!(ctx, Key(language, region), "hello").unwrap().as_str().unwrap())
105}
106
107#[launch]
108fn rocket() -> _ {
109    rocket::build()
110        .attach(static_json_gettext_build_for_rocket!(
111            key!("en");
112            key!("en") => "langs/en_US.json",
113            key!("zh_TW") => "langs/zh_TW.json",
114        ))
115        .mount("/", routes![index])
116}
117```
118*/
119
120pub extern crate serde_json;
121
122#[cfg(feature = "langid")]
123pub extern crate unic_langid;
124
125#[doc(hidden)]
126pub extern crate manifest_dir_macros;
127
128mod json_get_text_build_errors;
129mod macros;
130mod value;
131
132#[cfg(all(debug_assertions, feature = "rocket"))]
133mod mutate;
134
135#[cfg(feature = "langid")]
136mod key_copy;
137
138#[cfg(not(feature = "langid"))]
139mod key_string;
140
141pub use json_get_text_build_errors::*;
142#[cfg(feature = "langid")]
143pub use key_copy::*;
144#[cfg(not(feature = "langid"))]
145pub use key_string::*;
146#[cfg(all(debug_assertions, feature = "rocket"))]
147use mutate::DebuggableMutate;
148#[cfg(any(feature = "language", feature = "region"))]
149pub use unic_langid::parser::ParserError;
150#[cfg(feature = "language_region_pair")]
151pub use unic_langid::LanguageIdentifierError;
152pub use value::*;