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",
13    "langs/en_US.json",
14    "zh_TW",
15    "langs/zh_TW.json"
16)
17.unwrap();
18
19println!("{:?}", ctx);
20```
21**/
22#[macro_export]
23macro_rules! static_json_gettext_build {
24    ( $default_key:expr; $($key:expr => $path:expr), * $(,)* ) => {
25        {
26            let mut builder = $crate::JSONGetText::build($default_key);
27
28            $(
29                builder.add_json($key, include_str!($crate::manifest_dir_macros::path!($path))).unwrap();
30            )*
31
32            builder.build()
33        }
34    };
35}
36
37/**
38Used for getting single or multiple text from context.
39
40```ignore
41#[macro_use] extern crate json_gettext;
42
43let ctx = static_json_gettext_build!(
44    "en_US",
45    "en_US",
46    "langs/en_US.json",
47    "zh_TW",
48    "langs/zh_TW.json"
49)
50.unwrap();
51
52assert_eq!("Hello, world!", get_text!(ctx, "hello").unwrap());
53assert_eq!("哈囉,世界!", get_text!(ctx, "zh_TW", "hello").unwrap());
54```
55*/
56#[macro_export]
57macro_rules! get_text {
58    ( $ctx:ident, $text:expr ) => {
59        {
60            $ctx.get_text($text)
61        }
62    };
63    ( $ctx:ident, $key:expr, $text:expr ) => {
64        {
65            $ctx.get_text_with_key($key, $text)
66        }
67    };
68    ( $ctx:ident, $key:expr, $text:expr, $($text_array:expr), + ) => {
69        {
70            let mut text_array = vec![$text];
71
72            $(
73                {
74                    text_array.push($text_array);
75                }
76            )*
77
78            $ctx.get_multiple_text_with_key($key, &text_array)
79        }
80    };
81}