@@ -11,7 +11,7 @@ use std::error::Error;
1111use  std:: fmt; 
1212use  std:: fs; 
1313use  std:: io; 
14- use  std:: path:: Path ; 
14+ use  std:: path:: { Path ,   PathBuf } ; 
1515use  tracing:: { instrument,  trace} ; 
1616
1717#[ cfg( parallel_compiler) ]  
@@ -45,7 +45,7 @@ pub enum TranslationBundleError {
4545    /// Failed to add `FluentResource` to `FluentBundle`. 
4646     AddResource ( FluentError ) , 
4747    /// `$sysroot/share/locale/$locale` does not exist. 
48-      MissingLocale ( io :: Error ) , 
48+      MissingLocale , 
4949    /// Cannot read directory entries of `$sysroot/share/locale/$locale`. 
5050     ReadLocalesDir ( io:: Error ) , 
5151    /// Cannot read directory entry of `$sysroot/share/locale/$locale`. 
@@ -62,9 +62,7 @@ impl fmt::Display for TranslationBundleError {
6262                write ! ( f,  "could not parse ftl file: {}" ,  e) 
6363            } 
6464            TranslationBundleError :: AddResource ( e)  => write ! ( f,  "failed to add resource: {}" ,  e) , 
65-             TranslationBundleError :: MissingLocale ( e)  => { 
66-                 write ! ( f,  "missing locale directory: {}" ,  e) 
67-             } 
65+             TranslationBundleError :: MissingLocale  => write ! ( f,  "missing locale directory" ) , 
6866            TranslationBundleError :: ReadLocalesDir ( e)  => { 
6967                write ! ( f,  "could not read locales dir: {}" ,  e) 
7068            } 
@@ -84,7 +82,7 @@ impl Error for TranslationBundleError {
8482            TranslationBundleError :: ReadFtl ( e)  => Some ( e) , 
8583            TranslationBundleError :: ParseFtl ( e)  => Some ( e) , 
8684            TranslationBundleError :: AddResource ( e)  => Some ( e) , 
87-             TranslationBundleError :: MissingLocale ( e )  => Some ( e ) , 
85+             TranslationBundleError :: MissingLocale  => None , 
8886            TranslationBundleError :: ReadLocalesDir ( e)  => Some ( e) , 
8987            TranslationBundleError :: ReadLocalesDirEntry ( e)  => Some ( e) , 
9088            TranslationBundleError :: LocaleIsNotDir  => None , 
@@ -113,7 +111,8 @@ impl From<Vec<FluentError>> for TranslationBundleError {
113111/// (overriding any conflicting messages). 
114112#[ instrument( level = "trace" ) ]  
115113pub  fn  fluent_bundle ( 
116-     sysroot :  & Path , 
114+     mut  user_provided_sysroot :  Option < PathBuf > , 
115+     mut  sysroot_candidates :  Vec < PathBuf > , 
117116    requested_locale :  Option < LanguageIdentifier > , 
118117    additional_ftl_path :  Option < & Path > , 
119118    with_directionality_markers :  bool , 
@@ -140,33 +139,43 @@ pub fn fluent_bundle(
140139
141140    // If the user requests the default locale then don't try to load anything. 
142141    if  !requested_fallback_locale && let  Some ( requested_locale)  = requested_locale { 
143-         let  mut  sysroot = sysroot. to_path_buf ( ) ; 
144-         sysroot. push ( "share" ) ; 
145-         sysroot. push ( "locale" ) ; 
146-         sysroot. push ( requested_locale. to_string ( ) ) ; 
147-         trace ! ( ?sysroot) ; 
148- 
149-         let  _ = sysroot. try_exists ( ) . map_err ( TranslationBundleError :: MissingLocale ) ?; 
150- 
151-         if  !sysroot. is_dir ( )  { 
152-             return  Err ( TranslationBundleError :: LocaleIsNotDir ) ; 
153-         } 
154- 
155-         for  entry in  sysroot. read_dir ( ) . map_err ( TranslationBundleError :: ReadLocalesDir ) ? { 
156-             let  entry = entry. map_err ( TranslationBundleError :: ReadLocalesDirEntry ) ?; 
157-             let  path = entry. path ( ) ; 
158-             trace ! ( ?path) ; 
159-             if  path. extension ( ) . and_then ( |s| s. to_str ( ) )  != Some ( "ftl" )  { 
142+         let  mut  found_resources = false ; 
143+         for  sysroot in  user_provided_sysroot. iter_mut ( ) . chain ( sysroot_candidates. iter_mut ( ) )  { 
144+             sysroot. push ( "share" ) ; 
145+             sysroot. push ( "locale" ) ; 
146+             sysroot. push ( requested_locale. to_string ( ) ) ; 
147+             trace ! ( ?sysroot) ; 
148+ 
149+             if  !sysroot. exists ( )  { 
160150                trace ! ( "skipping" ) ; 
161151                continue ; 
162152            } 
163153
164-             let  resource_str =
165-                 fs:: read_to_string ( path) . map_err ( TranslationBundleError :: ReadFtl ) ?; 
166-             let  resource =
167-                 FluentResource :: try_new ( resource_str) . map_err ( TranslationBundleError :: from) ?; 
168-             trace ! ( ?resource) ; 
169-             bundle. add_resource ( resource) . map_err ( TranslationBundleError :: from) ?; 
154+             if  !sysroot. is_dir ( )  { 
155+                 return  Err ( TranslationBundleError :: LocaleIsNotDir ) ; 
156+             } 
157+ 
158+             for  entry in  sysroot. read_dir ( ) . map_err ( TranslationBundleError :: ReadLocalesDir ) ? { 
159+                 let  entry = entry. map_err ( TranslationBundleError :: ReadLocalesDirEntry ) ?; 
160+                 let  path = entry. path ( ) ; 
161+                 trace ! ( ?path) ; 
162+                 if  path. extension ( ) . and_then ( |s| s. to_str ( ) )  != Some ( "ftl" )  { 
163+                     trace ! ( "skipping" ) ; 
164+                     continue ; 
165+                 } 
166+ 
167+                 let  resource_str =
168+                     fs:: read_to_string ( path) . map_err ( TranslationBundleError :: ReadFtl ) ?; 
169+                 let  resource =
170+                     FluentResource :: try_new ( resource_str) . map_err ( TranslationBundleError :: from) ?; 
171+                 trace ! ( ?resource) ; 
172+                 bundle. add_resource ( resource) . map_err ( TranslationBundleError :: from) ?; 
173+                 found_resources = true ; 
174+             } 
175+         } 
176+ 
177+         if  !found_resources { 
178+             return  Err ( TranslationBundleError :: MissingLocale ) ; 
170179        } 
171180    } 
172181
0 commit comments