diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-09-11 12:24:35 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-12-10 18:30:28 +0000 |
| commit | c4d3e9b19cefe06200f824bf41bce8c4537194f9 (patch) | |
| tree | 2600b3f450f16952f395a69a74f8041f11addbf3 /android | |
| parent | e9ab57df497f40050959d7ed05dbc15a8b0191bb (diff) | |
| download | mullvadvpn-c4d3e9b19cefe06200f824bf41bce8c4537194f9.tar.xz mullvadvpn-c4d3e9b19cefe06200f824bf41bce8c4537194f9.zip | |
Create `PluralForm` type
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/gettext.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/android/translations-converter/src/gettext.rs b/android/translations-converter/src/gettext.rs index db1187ca0a..d9fb31af9a 100644 --- a/android/translations-converter/src/gettext.rs +++ b/android/translations-converter/src/gettext.rs @@ -18,6 +18,16 @@ 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 { @@ -111,6 +121,28 @@ 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) |
