@@ -9,10 +9,12 @@ use crate::error::UError;
99use fluent:: { FluentArgs , FluentBundle , FluentResource } ;
1010use fluent_syntax:: parser:: ParserError ;
1111
12+ use std:: cell:: Cell ;
1213use std:: fs;
1314use std:: path:: { Path , PathBuf } ;
1415use std:: str:: FromStr ;
1516use std:: sync:: OnceLock ;
17+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
1618
1719use os_display:: Quotable ;
1820use thiserror:: Error ;
@@ -108,6 +110,7 @@ impl Localizer {
108110
109111// Global localizer stored in thread-local OnceLock
110112thread_local ! {
113+ static LOCALIZER_IS_SET : Cell <bool > = const { Cell :: new( false ) } ;
111114 static LOCALIZER : OnceLock <Localizer > = const { OnceLock :: new( ) } ;
112115}
113116
@@ -407,14 +410,19 @@ fn detect_system_locale() -> Result<LanguageIdentifier, LocalizationError> {
407410/// }
408411/// ```
409412pub fn setup_localization ( p : & str ) -> Result < ( ) , LocalizationError > {
413+ // bypass heavy localization
414+ if LOCALIZER_IS_SET . with ( Cell :: get) {
415+ return Ok ( ( ) ) ;
416+ }
417+
410418 let locale = detect_system_locale ( ) . unwrap_or_else ( |_| {
411419 LanguageIdentifier :: from_str ( DEFAULT_LOCALE ) . expect ( "Default locale should always be valid" )
412420 } ) ;
413421
414422 // Load common strings along with utility-specific strings
415423 if let Ok ( locales_dir) = get_locales_dir ( p) {
416424 // Load both utility-specific and common strings
417- init_localization ( & locale, & locales_dir, p)
425+ init_localization ( & locale, & locales_dir, p) ? ;
418426 } else {
419427 // No locales directory found, use embedded English with common strings directly
420428 let default_locale = LanguageIdentifier :: from_str ( DEFAULT_LOCALE )
@@ -426,8 +434,9 @@ pub fn setup_localization(p: &str) -> Result<(), LocalizationError> {
426434 lock. set ( localizer)
427435 . map_err ( |_| LocalizationError :: Bundle ( "Localizer already initialized" . into ( ) ) )
428436 } ) ?;
429- Ok ( ( ) )
430437 }
438+ LOCALIZER_IS_SET . with ( |f| f. set ( true ) ) ;
439+ Ok ( ( ) )
431440}
432441
433442#[ cfg( not( debug_assertions) ) ]
0 commit comments