Fix syntax errors in bundle.rs
This commit is contained in:
parent
5ed6ee7988
commit
57fcb6fef2
1 changed files with 37 additions and 31 deletions
|
|
@ -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::<i64>() {
|
||||
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]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue