Fall back to single message if variations don't exist for localisation

This commit is contained in:
Joshua Barretto 2023-04-11 19:06:40 +01:00
parent 85a3d0bec4
commit c69307557d

View File

@ -116,7 +116,9 @@ impl Language {
let msg = bundle.get_message(key)?;
let mut attrs = msg.attributes();
if attrs.len() != 0 {
let mut errs = Vec::new();
let msg = if attrs.len() != 0 {
let idx = usize::from(seed) % attrs.len();
// unwrap is ok here, because idx is bound to attrs.len()
// by using modulo operator.
@ -134,16 +136,17 @@ impl Language {
// * len = 0
// * no matter what seed is, we return None in code above
let variation = attrs.nth(idx).unwrap();
let mut errs = Vec::new();
let msg = bundle.format_pattern(variation.value(), args, &mut errs);
for err in errs {
tracing::error!("err: {err} for {key}");
}
Some(msg)
bundle.format_pattern(variation.value(), args, &mut errs)
} else {
None
// Fall back to single message if there are no attributes
bundle.format_pattern(msg.value()?, args, &mut errs)
};
for err in errs {
tracing::error!("err: {err} for {key}");
}
Some(msg)
}
}