Skip to content

Commit 1ed7433

Browse files
authored
Merge pull request #8549 from cakebaker/uucore_locale_reduce_code_duplication
uucore/locale: move duplicate code to closure
2 parents c029ddb + 23592ac commit 1ed7433

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/uucore/src/lib/mods/locale.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,20 @@ fn create_bundle(
140140
// Disable Unicode directional isolate characters
141141
bundle.set_use_isolating(false);
142142

143-
// Load common strings from uucore locales directory
144-
if let Some(common_dir) = find_uucore_locales_dir(locales_dir) {
145-
let common_locale_path = common_dir.join(format!("{locale}.ftl"));
146-
if let Ok(common_ftl) = fs::read_to_string(&common_locale_path) {
147-
if let Ok(common_resource) = FluentResource::try_new(common_ftl) {
148-
bundle.add_resource_overriding(common_resource);
149-
}
143+
let mut try_add_resource_from = |dir_opt: Option<std::path::PathBuf>| {
144+
if let Some(resource) = dir_opt
145+
.map(|dir| dir.join(format!("{locale}.ftl")))
146+
.and_then(|locale_path| fs::read_to_string(locale_path).ok())
147+
.and_then(|ftl| fluent_bundle::FluentResource::try_new(ftl).ok())
148+
{
149+
bundle.add_resource_overriding(resource);
150150
}
151-
}
151+
};
152152

153+
// Load common strings from uucore locales directory
154+
try_add_resource_from(find_uucore_locales_dir(locales_dir));
153155
// Then, try to load utility-specific strings from the utility's locale directory
154-
let util_locales_dir = get_locales_dir(util_name).ok();
155-
if let Some(util_dir) = util_locales_dir {
156-
let util_locale_path = util_dir.join(format!("{locale}.ftl"));
157-
if let Ok(util_ftl) = fs::read_to_string(&util_locale_path) {
158-
if let Ok(util_resource) = FluentResource::try_new(util_ftl) {
159-
bundle.add_resource_overriding(util_resource);
160-
}
161-
}
162-
}
156+
try_add_resource_from(get_locales_dir(util_name).ok());
163157

164158
// If we have at least one resource, return the bundle
165159
if bundle.has_message("common-error") || bundle.has_message(&format!("{util_name}-about")) {

0 commit comments

Comments
 (0)