soft fail when key doesn't exist
This commit is contained in:
parent
54eb6206c9
commit
d97d413d7a
1 changed files with 25 additions and 6 deletions
|
@ -78,14 +78,24 @@ impl tera::Function for Localization {
|
||||||
let bundle = self.bundles.get(&locale)
|
let bundle = self.bundles.get(&locale)
|
||||||
.ok_or_else(|| tera::Error::msg(format!("localize: Could not find locale {locale}")))?;
|
.ok_or_else(|| tera::Error::msg(format!("localize: Could not find locale {locale}")))?;
|
||||||
|
|
||||||
let message = bundle.get_message(msg)
|
let message = if let Some(message) = bundle.get_message(msg) {
|
||||||
.ok_or_else(|| tera::Error::msg(format!("localize: Could not find 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 {
|
let pattern = if let Some(attribute) = attribute {
|
||||||
message.get_attribute(attribute)
|
let pattern = message.get_attribute(attribute)
|
||||||
.map(|attribute| attribute.value())
|
.map(|attribute| attribute.value());
|
||||||
.ok_or_else(|| tera::Error::msg(format!("localize: Attribute {msg}.{attribute} has no 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 {
|
} else {
|
||||||
message.value()
|
message.value()
|
||||||
.ok_or_else(|| tera::Error::msg(format!("localize: Message {msg} has no 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 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 {
|
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)?);
|
msg_args.set(key, fluent_value_from_tera(value, key)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue