@@ -11,7 +11,7 @@ use std::error::Error;
11
11
use std:: fmt;
12
12
use std:: fs;
13
13
use std:: io;
14
- use std:: path:: Path ;
14
+ use std:: path:: { Path , PathBuf } ;
15
15
use tracing:: { instrument, trace} ;
16
16
17
17
#[ cfg( parallel_compiler) ]
@@ -45,7 +45,7 @@ pub enum TranslationBundleError {
45
45
/// Failed to add `FluentResource` to `FluentBundle`.
46
46
AddResource ( FluentError ) ,
47
47
/// `$sysroot/share/locale/$locale` does not exist.
48
- MissingLocale ( io :: Error ) ,
48
+ MissingLocale ,
49
49
/// Cannot read directory entries of `$sysroot/share/locale/$locale`.
50
50
ReadLocalesDir ( io:: Error ) ,
51
51
/// Cannot read directory entry of `$sysroot/share/locale/$locale`.
@@ -62,9 +62,7 @@ impl fmt::Display for TranslationBundleError {
62
62
write ! ( f, "could not parse ftl file: {}" , e)
63
63
}
64
64
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" ) ,
68
66
TranslationBundleError :: ReadLocalesDir ( e) => {
69
67
write ! ( f, "could not read locales dir: {}" , e)
70
68
}
@@ -84,7 +82,7 @@ impl Error for TranslationBundleError {
84
82
TranslationBundleError :: ReadFtl ( e) => Some ( e) ,
85
83
TranslationBundleError :: ParseFtl ( e) => Some ( e) ,
86
84
TranslationBundleError :: AddResource ( e) => Some ( e) ,
87
- TranslationBundleError :: MissingLocale ( e ) => Some ( e ) ,
85
+ TranslationBundleError :: MissingLocale => None ,
88
86
TranslationBundleError :: ReadLocalesDir ( e) => Some ( e) ,
89
87
TranslationBundleError :: ReadLocalesDirEntry ( e) => Some ( e) ,
90
88
TranslationBundleError :: LocaleIsNotDir => None ,
@@ -113,7 +111,8 @@ impl From<Vec<FluentError>> for TranslationBundleError {
113
111
/// (overriding any conflicting messages).
114
112
#[ instrument( level = "trace" ) ]
115
113
pub fn fluent_bundle (
116
- sysroot : & Path ,
114
+ mut user_provided_sysroot : Option < PathBuf > ,
115
+ mut sysroot_candidates : Vec < PathBuf > ,
117
116
requested_locale : Option < LanguageIdentifier > ,
118
117
additional_ftl_path : Option < & Path > ,
119
118
with_directionality_markers : bool ,
@@ -140,33 +139,43 @@ pub fn fluent_bundle(
140
139
141
140
// If the user requests the default locale then don't try to load anything.
142
141
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 ( ) {
160
150
trace ! ( "skipping" ) ;
161
151
continue ;
162
152
}
163
153
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 ) ;
170
179
}
171
180
}
172
181
0 commit comments