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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use inflector::Inflector;
use lazy_static::lazy_static;
use serde_json::Value;
use std::env;
use std::sync::{Arc, RwLock};
use string_template::Template;
use structs::I18n;

pub use structs::I18nConfig;

mod structs;

static DIR_KEY: &'static str = "INTL_RS_DIR";

static DEFAULT_LANG_KEY: &'static str = "INTL_RS_LANG";

lazy_static! {
    pub static ref I18N: Arc<RwLock<I18n>> = {
        let dir = env::var(DIR_KEY).unwrap_or("src/languages".to_owned());
        let lang = env::var(DEFAULT_LANG_KEY).unwrap_or("zh_CN".to_owned());
        let inner = I18n::init(dir, lang);
        Arc::new(RwLock::new(inner))
    };
}

///find the optimal locale
pub fn find_optimal_locale<S: Into<String>>(locale: S, fallback: bool) -> Option<String> {
    let borrow = I18N.read().unwrap();
    let locale = locale.into();
    if !fallback && !borrow.inner.contains_key(&locale) {
        None
    } else {
        let mut common_result: Option<String> = None;
        let mut similar_result: Option<String> = None;
        let snake_locale = locale.as_str().to_snake_case();
        let locale_scope = locale.split(|c| c == '_' || c == '-').collect::<Vec<_>>()[0];
        for (key, _) in borrow.inner.iter() {
            let snake_key = key.as_str().to_snake_case();
            //en_US.json en-US.json completely match en_US.json when fallback flag is true;
            if snake_key == snake_locale {
                return Some(key.to_owned());
            }
            let key_infos = key.split(|c| c == '_' || c == '-').collect::<Vec<_>>();
            let scope = key_infos[0];
            if scope != locale_scope {
                continue;
            }
            // en.json is the common match of en_US.json,en_US.json is the similar match of en_UK.json
            if key_infos.len() == 1 {
                common_result = Some(key.to_owned());
            } else {
                similar_result = Some(key.to_owned());
            }
        }
        if common_result.is_some() {
            common_result
        } else {
            similar_result
        }
    }
}

///locale priority options.locale>current_locale>default_locale
///if result is null,use the null_placeholder
pub fn format_message<'a, S: Into<String>, D: Into<String>>(
    key: S,
    default_message: Option<D>,
    options: Option<I18nConfig<'a>>,
) -> String {
    let key = key.into();
    let borrow = I18N.read().unwrap();
    let I18nConfig {
        locale,
        fallback,
        null_placeholder,
        args,
    } = options.unwrap_or_default();
    let locale = locale.unwrap_or({
        borrow
            .current_locale
            .as_ref()
            .unwrap_or(&borrow.default_locale)
            .to_owned()
    });
    let fallback = fallback.unwrap_or(borrow.fallback);
    let fallback_message = null_placeholder.unwrap_or(borrow.null_placeholder.to_owned());
    let default_message = default_message
        .map(|m| m.into())
        .unwrap_or(fallback_message);
    let template_string = match find_optimal_locale(locale, fallback) {
        None => default_message,
        Some(locale_key) => {
            let configs: &Value = &borrow.inner[&locale_key];
            match key
                .split('.')
                .fold(configs, |result: &Value, k| &result[&k])
            {
                Value::Null => default_message,
                other => other
                    .as_str()
                    .map(|other_str| other_str.to_string())
                    .unwrap_or(other.to_string()),
            }
        }
    };
    let template = Template::new(&template_string);
    template.render(&args.unwrap_or_default())
}

///disable the global fallback config
pub fn disable_fallback() {
    I18N.write().unwrap().fallback = false;
}

///enable the global fallback config
pub fn enable_fallback() {
    I18N.write().unwrap().fallback = true;
}

///set the current locale config
pub fn set_current_locale<S: Into<String>>(current_locale: S) {
    I18N.write().unwrap().current_locale = Some(current_locale.into());
}

///set the null placeholder config
pub fn set_null_placeholder<S: Into<String>>(null_placeholder: S) {
    I18N.write().unwrap().null_placeholder = null_placeholder.into();
}

#[macro_export]
macro_rules! t {
    ($key:expr) => {
        $crate::format_message($key, None as Option<String>, None)
    };
    ($key:expr,configs:$configs:expr) => {
        $crate::format_message($key, None as Option<String>, Some($configs))
    };
    ($key:expr,default:$default_message:expr) => {
        $crate::format_message($key, Some($default_message), None)
    };
    ($key:expr,args:$args:expr) => {
        $crate::format_message(
            $key,
            None as Option<String>,
            Some({
                let mut inner = I18nConfig::default();
                inner.args = Some($args);
                inner
            }),
        )
    };
    ($key:expr,default:$default_message:expr,args:$args:expr) => {
        $crate::format_message(
            $key,
            Some($default_message),
            Some({
                let mut inner = I18nConfig::default();
                inner.args = Some($args);
                inner
            }),
        )
    };
    ($key:expr,$default_message:expr,$configs:expr) => {
        $crate::format_message($key, Some($default_message), $configs)
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    #[test]
    fn i18n_can_find_optimal_locale() {
        env::set_var("INTL_RS_DIR", "languages");
        assert_eq!(find_optimal_locale("en_UK", true), Some("en".to_owned()));

        assert_eq!(find_optimal_locale("en-US", true), Some("en_US".to_owned()));

        assert_eq!(
            find_optimal_locale("en_US", false),
            Some("en_US".to_owned())
        );

        assert_eq!(find_optimal_locale("en_UK", false), None);
    }

    #[test]
    fn i18n_can_format_messages() {
        env::set_var("INTL_RS_DIR", "languages");
        let key = "hello.world";
        assert_eq!(t!(key), "你好,世界!");

        assert_eq!(
            t!("unknown key", default:"default message"),
            "default message"
        );

        //default to ensure fallback
        //and you can disable it by disable_fallback function
        let configs = I18nConfig {
            fallback: None,
            locale: Some("en".to_owned()),
            null_placeholder: None,
            args: None,
        };
        assert_eq!(t!(key, configs: configs), "Hello,World!");

        let configs = I18nConfig {
            fallback: Some(true),
            locale: Some("en_UK".to_owned()),
            null_placeholder: None,
            args: None,
        };
        assert_eq!(t!(key, configs: configs), "Hello,World!");

        //change the default null placeholder
        let configs = I18nConfig {
            fallback: Some(true),
            locale: Some("en_UK".to_owned()),
            null_placeholder: Some("".to_owned()),
            args: None,
        };
        assert_eq!(t!("unknown key", configs: configs), "");
        //render template
        let mut args: HashMap<&str, &str> = HashMap::new();
        args.insert("name", "Donald Trump");

        let configs = I18nConfig {
            fallback: Some(true),
            locale: Some("en_UK".to_owned()),
            null_placeholder: Some("".to_owned()),
            args: Some(args.clone()),
        };
        assert_eq!(
            t!("hello.somebody", configs: configs),
            "Hello,Donald Trump!"
        );

        assert_eq!(
            t!("unknown key",default:"Hey,{{name}}!", args: args.clone()),
            "Hey,Donald Trump!"
        );

        let mut args: HashMap<&str, &str> = HashMap::new();
        args.insert("name", "唐纳德·川普");
        assert_eq!(
            t!("hello.somebody", args: args.clone()),
            "你好,唐纳德·川普!"
        );
    }
}