diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index 49470cf..d8b2ae3 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -324,6 +324,8 @@ fn generate_code( quote! { use rust_i18n::{BackendExt, CowStr, MinifyKey}; use std::borrow::Cow; + use std::sync::Mutex; + use rust_i18n::once_cell::sync::Lazy; /// I18n backend instance /// @@ -334,11 +336,17 @@ fn generate_code( #(#all_translations)* #extend_code - #default_locale + if *_RUST_I18N_INITIALIZED_DEFAULT_LOCALE.lock().unwrap() == false { + *_RUST_I18N_INITIALIZED_DEFAULT_LOCALE.lock().unwrap() = true; + #default_locale + } Box::new(backend) }); + /// To mark the default locale has been initialized + static _RUST_I18N_INITIALIZED_DEFAULT_LOCALE: Lazy> = Lazy::new(|| Mutex::new(false)); + static _RUST_I18N_FALLBACK_LOCALE: Option<&[&'static str]> = #fallback; static _RUST_I18N_MINIFY_KEY: bool = #minify_key; static _RUST_I18N_MINIFY_KEY_LEN: usize = #minify_key_len; diff --git a/crates/support/src/backend.rs b/crates/support/src/backend.rs index 54fd031..ff9c38a 100644 --- a/crates/support/src/backend.rs +++ b/crates/support/src/backend.rs @@ -74,10 +74,7 @@ impl SimpleBackend { .map(|(k, v)| ((*k).into(), (*v).into())) .collect::>(); - let trs = self - .translations - .entry(locale.into()) - .or_insert(HashMap::new()); + let trs = self.translations.entry(locale.into()).or_default(); trs.extend(data); } } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 9e88084..18fbd9f 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -315,4 +315,12 @@ mod tests { "This is missing key fallbacked to en." ); } + + #[test] + fn test_set_locale() { + rust_i18n::set_locale("zh-CN"); + for _ in 0..5 { + assert_eq!(t!("hello"), "Bar - 你好世界!"); + } + } }