Skip to main content

json_gettext/macros/
mod.rs

1#[cfg(feature = "rocket")]
2mod rocket_feature;
3
4/**
5Used for including json files into your executable binary file for building a `JSONGetText` instance.
6
7```ignore
8#[macro_use] extern crate json_gettext;
9
10let ctx = static_json_gettext_build!(
11    "en_US";
12    "en_US" => "langs/en_US.json",
13    "zh_TW" => "langs/zh_TW.json",
14)
15.unwrap();
16
17println!("{:?}", ctx);
18```
19**/
20#[macro_export]
21macro_rules! static_json_gettext_build {
22    ( $default_key:expr; $($key:expr => $path:expr), * $(,)* ) => {
23        (|| -> ::core::result::Result<$crate::JSONGetText<'static>, $crate::JSONGetTextBuildError> {
24            let mut builder = $crate::JSONGetText::build($default_key);
25
26            $(
27                builder.add_json($key, include_str!($crate::manifest_dir_macros::path!($path)))?;
28            )*
29
30            builder.build()
31        })()
32    };
33}
34
35/**
36Used for getting single or multiple text from context.
37
38```ignore
39#[macro_use] extern crate json_gettext;
40
41let ctx = static_json_gettext_build!(
42    "en_US";
43    "en_US" => "langs/en_US.json",
44    "zh_TW" => "langs/zh_TW.json",
45)
46.unwrap();
47
48assert_eq!("Hello, world!", get_text!(ctx, "hello").unwrap());
49assert_eq!("哈囉,世界!", get_text!(ctx, "zh_TW", "hello").unwrap());
50```
51*/
52#[macro_export]
53macro_rules! get_text {
54    ( $ctx:expr, $text:expr ) => {
55        {
56            $ctx.get_text($text)
57        }
58    };
59    ( $ctx:expr, $key:expr, $text:expr ) => {
60        {
61            $ctx.get_text_with_key($key, $text)
62        }
63    };
64    ( $ctx:expr, $key:expr, $text:expr, $($text_array:expr), + ) => {
65        {
66            $ctx.get_multiple_text_with_key($key, &[$text, $($text_array),+])
67        }
68    };
69}