diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-09-11 00:03:43 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-12-10 18:30:28 +0000 |
| commit | e9ab57df497f40050959d7ed05dbc15a8b0191bb (patch) | |
| tree | 3ef118621f6cf88f3abaff6bf4bb5196690c59cc /android | |
| parent | deae5310089c99d45754079d0ca381ef14c6d5e7 (diff) | |
| download | mullvadvpn-e9ab57df497f40050959d7ed05dbc15a8b0191bb.tar.xz mullvadvpn-e9ab57df497f40050959d7ed05dbc15a8b0191bb.zip | |
Parse translated gettext plurals
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/gettext.rs | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/android/translations-converter/src/gettext.rs b/android/translations-converter/src/gettext.rs index 1f0f19fe9c..db1187ca0a 100644 --- a/android/translations-converter/src/gettext.rs +++ b/android/translations-converter/src/gettext.rs @@ -1,8 +1,10 @@ use lazy_static::lazy_static; use regex::Regex; use std::{ + collections::BTreeMap, fs::{File, OpenOptions}, io::{self, BufRead, BufReader, BufWriter, Write}, + mem, path::Path, }; @@ -41,6 +43,8 @@ impl Translation { pub fn from_file(file_path: impl AsRef<Path>) -> Self { let mut entries = Vec::new(); let mut current_id = None; + let mut current_plural_id = None; + let mut variants = BTreeMap::new(); let file = BufReader::new(File::open(file_path).expect("Failed to open gettext file")); for line in file.lines() { @@ -49,16 +53,48 @@ impl Translation { if let Some(msg_id) = parse_line(line, "msgid \"", "\"") { current_id = Some(normalize(msg_id)); + } else if let Some(translation) = parse_line(line, "msgstr \"", "\"") { + if let Some(id) = current_id.take() { + let value = MsgValue::from(normalize(translation)); + + entries.push(MsgEntry { id, value }); + } + + current_id = None; + current_plural_id = None; + } else if let Some(plural_id) = parse_line(line, "msgid_plural \"", "\"") { + current_plural_id = Some(normalize(plural_id)); + } else if let Some(plural_translation) = parse_line(line, "msgstr[", "\"") { + let variant_id_end = plural_translation + .chars() + .position(|character| character == ']') + .expect("Invalid plural msgstr"); + let variant_id: usize = plural_translation[..variant_id_end] + .parse() + .expect("Invalid variant index"); + let variant_msg = parse_line(&plural_translation[variant_id_end..], "] \"", "") + .expect("Invalid plural msgstr"); + + variants.insert(variant_id, normalize(variant_msg)); } else { - if let Some(translation) = parse_line(line, "msgstr \"", "\"") { - if let Some(id) = current_id.take() { - let value = MsgValue::from(normalize(translation)); + if let Some(plural_id) = current_plural_id.take() { + let id = current_id.take().expect("Missing msgid for plural message"); + let values = mem::replace(&mut variants, BTreeMap::new()) + .into_iter() + .enumerate() + .inspect(|(index, (variant_id, _))| { + assert_eq!(index, variant_id, "Unexpected variant ID for plural msgstr") + }) + .map(|(_, (_, value))| value) + .collect(); + let value = MsgValue::Plural { plural_id, values }; - entries.push(MsgEntry { id, value }); - } + entries.push(MsgEntry { id, value }); } current_id = None; + current_plural_id = None; + variants.clear(); } } |
