soft fail when key doesn't exist

This commit is contained in:
Hannaeko 2025-06-10 14:24:46 +01:00
parent 54eb6206c9
commit d97d413d7a

View file

@ -78,14 +78,24 @@ impl tera::Function for Localization {
let bundle = self.bundles.get(&locale)
.ok_or_else(|| tera::Error::msg(format!("localize: Could not find locale {locale}")))?;
let message = bundle.get_message(msg)
.ok_or_else(|| tera::Error::msg(format!("localize: Could not find message {msg}")))?;
let message = if let Some(message) = bundle.get_message(msg) {
message
} else {
eprintln!("[warn] localize: could not find message '{msg}'");
return Ok(tera::Value::from(format!("{{{msg}}}")));
};
let pattern = if let Some(attribute) = attribute {
message.get_attribute(attribute)
.map(|attribute| attribute.value())
.ok_or_else(|| tera::Error::msg(format!("localize: Attribute {msg}.{attribute} has no value")))?
let pattern = message.get_attribute(attribute)
.map(|attribute| attribute.value());
if let Some(pattern) = pattern {
pattern
} else {
eprintln!("[warn] localize: could not find message '{msg}.{attribute}'");
return Ok(tera::Value::from(format!("{{{msg}.{attribute}}}")));
}
} else {
message.value()
.ok_or_else(|| tera::Error::msg(format!("localize: Message {msg} has no value")))?
@ -93,8 +103,17 @@ impl tera::Function for Localization {
let mut msg_args = FluentArgs::new();
let extra_args = args.get("extra_args")
.and_then(|extra_args| extra_args.as_object());
if let Some(extra_args) = extra_args {
for (key, value) in extra_args {
msg_args.set(key, fluent_value_from_tera(value, key)?);
}
}
for (key, value) in args {
if key != "msg" && key != "lang" && key != "attr" {
if key != "msg" && key != "lang" && key != "attr" && key != "extra_args" {
msg_args.set(key, fluent_value_from_tera(value, key)?);
}
}