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