Commit 6389570 1 parent 5d4b561 commit 6389570 Copy full SHA for 6389570
File tree 3 files changed +18
-5
lines changed
3 files changed +18
-5
lines changed Original file line number Diff line number Diff line change @@ -324,6 +324,8 @@ fn generate_code(
324
324
quote ! {
325
325
use rust_i18n:: { BackendExt , CowStr , MinifyKey } ;
326
326
use std:: borrow:: Cow ;
327
+ use std:: sync:: Mutex ;
328
+ use rust_i18n:: once_cell:: sync:: Lazy ;
327
329
328
330
/// I18n backend instance
329
331
///
@@ -334,11 +336,17 @@ fn generate_code(
334
336
#( #all_translations) *
335
337
#extend_code
336
338
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
+ }
338
343
339
344
Box :: new( backend)
340
345
} ) ;
341
346
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
+
342
350
static _RUST_I18N_FALLBACK_LOCALE: Option <& [ & ' static str ] > = #fallback;
343
351
static _RUST_I18N_MINIFY_KEY: bool = #minify_key;
344
352
static _RUST_I18N_MINIFY_KEY_LEN: usize = #minify_key_len;
Original file line number Diff line number Diff line change @@ -74,10 +74,7 @@ impl SimpleBackend {
74
74
. map ( |( k, v) | ( ( * k) . into ( ) , ( * v) . into ( ) ) )
75
75
. collect :: < HashMap < _ , _ > > ( ) ;
76
76
77
- let trs = self
78
- . translations
79
- . entry ( locale. into ( ) )
80
- . or_insert ( HashMap :: new ( ) ) ;
77
+ let trs = self . translations . entry ( locale. into ( ) ) . or_default ( ) ;
81
78
trs. extend ( data) ;
82
79
}
83
80
}
Original file line number Diff line number Diff line change @@ -315,4 +315,12 @@ mod tests {
315
315
"This is missing key fallbacked to en."
316
316
) ;
317
317
}
318
+
319
+ #[ test]
320
+ fn test_set_locale ( ) {
321
+ rust_i18n:: set_locale ( "zh-CN" ) ;
322
+ for _ in 0 ..5 {
323
+ assert_eq ! ( t!( "hello" ) , "Bar - 你好世界!" ) ;
324
+ }
325
+ }
318
326
}
You can’t perform that action at this time.
0 commit comments