From 57fcb6fef215b6dc1367086025f7686834571798 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Tue, 27 Jan 2026 14:47:32 -0300 Subject: [PATCH] Fix syntax errors in bundle.rs --- src/i18n/bundle.rs | 68 +++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/src/i18n/bundle.rs b/src/i18n/bundle.rs index aac0e30..5e38210 100644 --- a/src/i18n/bundle.rs +++ b/src/i18n/bundle.rs @@ -88,19 +88,19 @@ impl LocaleBundle { .and_then(|n| n.to_str()) .ok_or_else(|| BotError::config("invalid locale directory name"))?; - .map_err(|e| locale = Locale::new(dir_name) .ok_or_else(|| BotError::config(format!("invalid locale: {dir_name}")))?; + let locale = Locale::new(dir_name) + .ok_or_else(|| BotError::config(format!("invalid locale: {dir_name}")))?; let mut translations = TranslationFile { - messages: HashMap - };.map_err(|e| - let entries = fs::read_dir(locale_dir).map_err(|e| { - BotError::config(format!("failed to read locale directory: {e}")) - })?; + messages: HashMap::new(), + }; + + let entries = fs::read_dir(locale_dir) + .map_err(|e| BotError::config(format!("failed to read locale directory: {e}")))?; for entry in entries { - let entry = entry.map_err(|e| { - BotError::config(format!("failed to read directory entry: {e}")) - })?; + let entry = entry + .map_err(|e| BotError::config(format!("failed to read directory entry: {e}")))?; let path = entry.path(); @@ -136,16 +136,17 @@ impl LocaleBundle { if file.starts_with(locale_str) && file.ends_with(".ftl") { if let Some(content_bytes) = EmbeddedLocales::get(&file) { if let Ok(content) = std::str::from_utf8(content_bytes.data.as_ref()) { - let file_translations = TranslationFile::parse(content); translations.merge(file_translations); } } } - .map_err(|e| + } + Ok(Self { locale, translations, - }) .map_err(|e| + }) + } fn get_message(&self, key: &str) -> Option<&String> { self.translations.get(key) @@ -166,7 +167,10 @@ impl I18nBundle { if !base.exists() { #[cfg(feature = "i18n")] { - log::info!("Locales directory not found at {}, trying embedded assets", base_path); + log::info!( + "Locales directory not found at {}, trying embedded assets", + base_path + ); return Self::load_embedded(); } #[cfg(not(feature = "i18n"))] @@ -178,14 +182,12 @@ impl I18nBundle { let mut bundles = HashMap::new(); let mut available = Vec::new(); - let entries = fs::read_dir(base).map_err(|e| { - BotError::config(format!("failed to read locales directory: {e}")) - })?; + let entries = fs::read_dir(base) + .map_err(|e| BotError::config(format!("failed to read locales directory: {e}")))?; for entry in entries { - let entry = entry.map_err(|e| { - BotError::config(format!("failed to read directory entry: {e}")) - })?; + let entry = entry + .map_err(|e| BotError::config(format!("failed to read directory entry: {e}")))?; let path = entry.path(); @@ -229,7 +231,11 @@ impl I18nBundle { seen_locales.insert(locale_str.to_string()); } Err(e) => { - log::warn!("failed to load embedded locale bundle {}: {}", locale_str, e); + log::warn!( + "failed to load embedded locale bundle {}: {}", + locale_str, + e + ); } } } @@ -326,14 +332,17 @@ impl I18nBundle { fn handle_plurals(template: &str, args: &MessageArgs) -> String { let mut result = template.to_string(); -(key, value) in args let count: i64 = value.parse().unwrap_ - let plural_pattern = format!("{{ ${key} ->"); - if let Some(start) = result.find(&plural_pattern) { - if let Some(end) = result[start..].find('}') { - let plural_block = &result[start..start + end + 1]; - let replacement = Self::select_plural_form(plural_block, count); - result = result.replace(plural_block, &replacement); + for (key, value) in args { + if let Ok(count) = value.parse::() { + let plural_pattern = format!("{{ ${key} ->"); + + if let Some(start) = result.find(&plural_pattern) { + if let Some(end) = result[start..].find('}') { + let plural_block = &result[start..start + end + 1]; + let replacement = Self::select_plural_form(plural_block, count); + result = result.replace(plural_block, &replacement); + } } } } @@ -397,10 +406,7 @@ world = World greeting = Hello, { $name }! "#; let file = TranslationFile::parse(content); - assert_eq!( - file.get("greeting"), - Some(&"Hello, { $name }!".to_string()) - ); + assert_eq!(file.get("greeting"), Some(&"Hello, { $name }!".to_string())); } #[test]