Refactor try_variaton and error handling in i18n

This commit is contained in:
juliancoffee 2022-08-07 14:39:04 +03:00
parent b5436872ed
commit c38d94e504
3 changed files with 37 additions and 35 deletions

8
Cargo.lock generated
View File

@ -1905,7 +1905,7 @@ dependencies = [
[[package]]
name = "fluent"
version = "0.16.0"
source = "git+https://github.com/juliancoffee/fluent-rs.git#c91d9f48b5a8734bd9183145320f5108526b2d1e"
source = "git+https://github.com/juliancoffee/fluent-rs.git?branch=patched#929cf9512de121cce9b4cbf1cb860cd3294a1cd9"
dependencies = [
"fluent-bundle",
"unic-langid",
@ -1914,7 +1914,7 @@ dependencies = [
[[package]]
name = "fluent-bundle"
version = "0.15.2"
source = "git+https://github.com/juliancoffee/fluent-rs.git#c91d9f48b5a8734bd9183145320f5108526b2d1e"
source = "git+https://github.com/juliancoffee/fluent-rs.git?branch=patched#929cf9512de121cce9b4cbf1cb860cd3294a1cd9"
dependencies = [
"fluent-langneg",
"fluent-syntax",
@ -1938,7 +1938,7 @@ dependencies = [
[[package]]
name = "fluent-syntax"
version = "0.11.0"
source = "git+https://github.com/juliancoffee/fluent-rs.git#c91d9f48b5a8734bd9183145320f5108526b2d1e"
source = "git+https://github.com/juliancoffee/fluent-rs.git?branch=patched#929cf9512de121cce9b4cbf1cb860cd3294a1cd9"
dependencies = [
"thiserror",
]
@ -2843,7 +2843,7 @@ dependencies = [
[[package]]
name = "intl-memoizer"
version = "0.5.1"
source = "git+https://github.com/juliancoffee/fluent-rs.git#c91d9f48b5a8734bd9183145320f5108526b2d1e"
source = "git+https://github.com/juliancoffee/fluent-rs.git?branch=patched#929cf9512de121cce9b4cbf1cb860cd3294a1cd9"
dependencies = [
"type-map",
"unic-langid",

View File

@ -12,9 +12,9 @@ ron = "0.7"
serde = { version = "1.0", features = ["derive"] }
# Localization
unic-langid = { version = "0.9"}
intl-memoizer = { git = "https://github.com/juliancoffee/fluent-rs.git"}
fluent = { git = "https://github.com/juliancoffee/fluent-rs.git"}
fluent-bundle = { git = "https://github.com/juliancoffee/fluent-rs.git"}
intl-memoizer = { git = "https://github.com/juliancoffee/fluent-rs.git", branch = "patched"}
fluent = { git = "https://github.com/juliancoffee/fluent-rs.git", branch = "patched"}
fluent-bundle = { git = "https://github.com/juliancoffee/fluent-rs.git", branch = "patched"}
# Utility
hashbrown = { version = "0.12", features = ["serde", "nightly"] }
deunicode = "1.0"

View File

@ -78,47 +78,49 @@ impl Language {
let mut errs = Vec::new();
let msg = bundle.format_pattern(msg.value()?, args, &mut errs);
for err in errs {
eprintln!("err: {err} for {key}");
tracing::error!("err: {err} for {key}");
}
Some(msg)
}
fn try_collect_attrs<'a>(
&'a self,
key: &str,
args: Option<&'a FluentArgs>,
) -> Option<Vec<Cow<str>>> {
let bundle = &self.bundle;
let msg = bundle.get_message(key)?;
let mut errs = Vec::new();
let mut attrs = Vec::new();
for attr in msg.attributes() {
let msg = bundle.format_pattern(attr.value(), args, &mut errs);
attrs.push(msg);
}
for err in errs {
eprintln!("err: {err} for {key}");
}
Some(attrs)
}
fn try_variation<'a>(
&'a self,
key: &str,
seed: u16,
args: Option<&'a FluentArgs>,
) -> Option<Cow<'a, str>> {
let mut attrs = self.try_collect_attrs(key, args)?;
let bundle = &self.bundle;
let msg = bundle.get_message(key)?;
let mut attrs = msg.attributes();
if attrs.is_empty() {
None
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.
//
// For example:
// (I)
// * attributes = [.x = 5, .y = 7, z. = 4]
// * len = 3
// * seed can be 12, 50, 1
// 12 % 3 = 0, attrs.skip(0) => first element
// 50 % 3 = 2, attrs.skip(2) => third element
// 1 % 3 = 1, attrs.skip(1) => second element
// (II)
// * attributes = []
// * 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)
} else {
let variation = attrs.swap_remove(usize::from(seed) % attrs.len());
Some(variation)
None
}
}
}