diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-10 17:25:33 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-19 12:13:36 +0000 |
| commit | 4da1dd0e776127b60e4b449dd4373d051a1ee065 (patch) | |
| tree | 755003806087fd6be7197dd5067e95e56f86062d /android | |
| parent | cfe2c6e272b2a7c4805711f596386522807633a4 (diff) | |
| download | mullvadvpn-4da1dd0e776127b60e4b449dd4373d051a1ee065.tar.xz mullvadvpn-4da1dd0e776127b60e4b449dd4373d051a1ee065.zip | |
Move `PluralForm` into a new module
Also replace the `panic` that could happen when an unknown plural
formula is used with returning `None`.
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/gettext/mod.rs | 38 | ||||
| -rw-r--r-- | android/translations-converter/src/gettext/plural_form.rs | 31 |
2 files changed, 35 insertions, 34 deletions
diff --git a/android/translations-converter/src/gettext/mod.rs b/android/translations-converter/src/gettext/mod.rs index ae71e1ed7c..438f281cef 100644 --- a/android/translations-converter/src/gettext/mod.rs +++ b/android/translations-converter/src/gettext/mod.rs @@ -1,4 +1,5 @@ mod msg_string; +mod plural_form; use self::msg_string::MsgString; use lazy_static::lazy_static; @@ -11,6 +12,8 @@ use std::{ path::Path, }; +pub use self::plural_form::PluralForm; + lazy_static! { static ref APOSTROPHE_VARIATION: Regex = Regex::new("’").unwrap(); static ref ESCAPED_DOUBLE_QUOTES: Regex = Regex::new(r#"\\""#).unwrap(); @@ -24,16 +27,6 @@ pub struct Translation { entries: Vec<MsgEntry>, } -/// Known plural forms. -#[derive(Clone, Copy, Debug)] -pub enum PluralForm { - Single, - SingularForOne, - SingularForZeroAndOne, - Polish, - Russian, -} - /// A message entry in a gettext translation file. #[derive(Clone, Debug)] pub struct MsgEntry { @@ -151,7 +144,7 @@ impl Translation { ["\"", header, "\\n\""] => { if parsing_header { if let Some(plural_formula) = parse_line(header, "Plural-Forms: ", ";") { - plural_form = Some(PluralForm::from_formula(plural_formula)); + plural_form = PluralForm::from_formula(plural_formula); } } } @@ -198,29 +191,6 @@ impl IntoIterator for Translation { } } -impl PluralForm { - /// Obtain an instance based on a known plural formula. - /// - /// Plural variants need to be obtained using a formula. However, some locales have known - /// formulas, so they can be represented as a known plural form. This constructor can return a - /// plural form based on the formulas that are known to be used in the project. - pub fn from_formula(formula: &str) -> Self { - match formula { - "nplurals=1; plural=0" => PluralForm::Single, - "nplurals=2; plural=(n != 1)" => PluralForm::SingularForOne, - "nplurals=2; plural=(n > 1)" => PluralForm::SingularForZeroAndOne, - "nplurals=4; plural=(n==1 ? 0 : (n%10>=2 && n%10<=4) && (n%100<12 || n%100>14) ? 1 : n!=1 && (n%10>=0 && n%10<=1) || (n%10>=5 && n%10<=9) || (n%100>=12 && n%100<=14) ? 2 : 3)" => { - PluralForm::Polish - } - "nplurals=4; plural=((n%10==1 && n%100!=11) ? 0 : ((n%10 >= 2 && n%10 <=4 && (n%100 < 12 || n%100 > 14)) ? 1 : ((n%10 == 0 || (n%10 >= 5 && n%10 <=9)) || (n%100 >= 11 && n%100 <= 14)) ? 2 : 3))" => { - PluralForm::Russian - } - other => panic!("Unknown plural formula: {}", other), - } - } -} - - impl From<String> for MsgValue { fn from(string: String) -> Self { MsgValue::Invariant(string.into()) diff --git a/android/translations-converter/src/gettext/plural_form.rs b/android/translations-converter/src/gettext/plural_form.rs new file mode 100644 index 0000000000..c55066c7b7 --- /dev/null +++ b/android/translations-converter/src/gettext/plural_form.rs @@ -0,0 +1,31 @@ +/// Known plural forms. +#[derive(Clone, Copy, Debug)] +pub enum PluralForm { + Single, + SingularForOne, + SingularForZeroAndOne, + Polish, + Russian, +} + +impl PluralForm { + /// Obtain an instance based on a known plural formula. + /// + /// Plural variants need to be obtained using a formula. However, some locales have known + /// formulas, so they can be represented as a known plural form. This constructor can return a + /// plural form based on the formulas that are known to be used in the project. + pub fn from_formula(formula: &str) -> Option<Self> { + match formula { + "nplurals=1; plural=0" => Some(PluralForm::Single), + "nplurals=2; plural=(n != 1)" => Some(PluralForm::SingularForOne), + "nplurals=2; plural=(n > 1)" => Some(PluralForm::SingularForZeroAndOne), + "nplurals=4; plural=(n==1 ? 0 : (n%10>=2 && n%10<=4) && (n%100<12 || n%100>14) ? 1 : n!=1 && (n%10>=0 && n%10<=1) || (n%10>=5 && n%10<=9) || (n%100>=12 && n%100<=14) ? 2 : 3)" => { + Some(PluralForm::Polish) + } + "nplurals=4; plural=((n%10==1 && n%100!=11) ? 0 : ((n%10 >= 2 && n%10 <=4 && (n%100 < 12 || n%100 > 14)) ? 1 : ((n%10 == 0 || (n%10 >= 5 && n%10 <=9)) || (n%100 >= 11 && n%100 <= 14)) ? 2 : 3))" => { + Some(PluralForm::Russian) + } + _ => None + } + } +} |
