Skip to content

Commit

Permalink
Fix lazy init always "en"
Browse files Browse the repository at this point in the history
Closes #87 #88

Co-authored-by: yk0n9 <[email protected]>
Co-authored-by: KKRainbow <[email protected]>
  • Loading branch information
3 people committed Aug 20, 2024
1 parent 07e4c65 commit 19e9145
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 9 additions & 1 deletion crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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<Mutex<bool>> = 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;
Expand Down
2 changes: 0 additions & 2 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ rust_i18n::i18n!(

#[cfg(test)]
mod tests {
use std::{thread, time::Duration};

use rust_i18n::t;
use rust_i18n_support::load_locales;

Expand Down

0 comments on commit 19e9145

Please sign in to comment.