Skip to content

Commit 19e9145

Browse files
huacnleeyk0n9KKRainbow
committed
Fix lazy init always "en"
Closes #87 #88 Co-authored-by: yk0n9 <ykonghao@icloud.com> Co-authored-by: KKRainbow <sijie.sun@smartx.com>
1 parent 07e4c65 commit 19e9145

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

crates/macro/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ fn generate_code(
324324
quote! {
325325
use rust_i18n::{BackendExt, CowStr, MinifyKey};
326326
use std::borrow::Cow;
327+
use std::sync::Mutex;
328+
use rust_i18n::once_cell::sync::Lazy;
327329

328330
/// I18n backend instance
329331
///
@@ -334,11 +336,17 @@ fn generate_code(
334336
#(#all_translations)*
335337
#extend_code
336338

337-
#default_locale
339+
if *_RUST_I18N_INITIALIZED_DEFAULT_LOCALE.lock().unwrap() == false {
340+
*_RUST_I18N_INITIALIZED_DEFAULT_LOCALE.lock().unwrap() = true;
341+
#default_locale
342+
}
338343

339344
Box::new(backend)
340345
});
341346

347+
/// To mark the default locale has been initialized
348+
static _RUST_I18N_INITIALIZED_DEFAULT_LOCALE: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));
349+
342350
static _RUST_I18N_FALLBACK_LOCALE: Option<&[&'static str]> = #fallback;
343351
static _RUST_I18N_MINIFY_KEY: bool = #minify_key;
344352
static _RUST_I18N_MINIFY_KEY_LEN: usize = #minify_key_len;

tests/integration_tests.rs

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ rust_i18n::i18n!(
3333

3434
#[cfg(test)]
3535
mod tests {
36-
use std::{thread, time::Duration};
37-
3836
use rust_i18n::t;
3937
use rust_i18n_support::load_locales;
4038

0 commit comments

Comments
 (0)