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]] [[package]]
name = "fluent" name = "fluent"
version = "0.16.0" 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 = [ dependencies = [
"fluent-bundle", "fluent-bundle",
"unic-langid", "unic-langid",
@ -1914,7 +1914,7 @@ dependencies = [
[[package]] [[package]]
name = "fluent-bundle" name = "fluent-bundle"
version = "0.15.2" 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 = [ dependencies = [
"fluent-langneg", "fluent-langneg",
"fluent-syntax", "fluent-syntax",
@ -1938,7 +1938,7 @@ dependencies = [
[[package]] [[package]]
name = "fluent-syntax" name = "fluent-syntax"
version = "0.11.0" 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 = [ dependencies = [
"thiserror", "thiserror",
] ]
@ -2843,7 +2843,7 @@ dependencies = [
[[package]] [[package]]
name = "intl-memoizer" name = "intl-memoizer"
version = "0.5.1" 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 = [ dependencies = [
"type-map", "type-map",
"unic-langid", "unic-langid",

View File

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

View File

@ -78,47 +78,49 @@ impl Language {
let mut errs = Vec::new(); let mut errs = Vec::new();
let msg = bundle.format_pattern(msg.value()?, args, &mut errs); let msg = bundle.format_pattern(msg.value()?, args, &mut errs);
for err in errs { for err in errs {
eprintln!("err: {err} for {key}"); tracing::error!("err: {err} for {key}");
} }
Some(msg) 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>( fn try_variation<'a>(
&'a self, &'a self,
key: &str, key: &str,
seed: u16, seed: u16,
args: Option<&'a FluentArgs>, args: Option<&'a FluentArgs>,
) -> Option<Cow<'a, str>> { ) -> 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() { if attrs.len() != 0 {
None 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 { } else {
let variation = attrs.swap_remove(usize::from(seed) % attrs.len()); None
Some(variation)
} }
} }
} }