diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-10 20:18:41 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-19 12:13:36 +0000 |
| commit | fe922d4188e93b2bf59aad0e33b136f9eeaa4302 (patch) | |
| tree | 44c7a3208e95a3722cdc0a2ef4ee2e0a7d63694c /android | |
| parent | 5acea54d49b3e44ab92a4e28d4e4e82a6859b142 (diff) | |
| download | mullvadvpn-fe922d4188e93b2bf59aad0e33b136f9eeaa4302.tar.xz mullvadvpn-fe922d4188e93b2bf59aad0e33b136f9eeaa4302.zip | |
Remove previous gettext string normalization
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/gettext/mod.rs | 30 | ||||
| -rw-r--r-- | android/translations-converter/src/main.rs | 12 |
2 files changed, 10 insertions, 32 deletions
diff --git a/android/translations-converter/src/gettext/mod.rs b/android/translations-converter/src/gettext/mod.rs index d3be28c4ef..06f50458f9 100644 --- a/android/translations-converter/src/gettext/mod.rs +++ b/android/translations-converter/src/gettext/mod.rs @@ -1,8 +1,6 @@ mod msg_string; mod plural_form; -use lazy_static::lazy_static; -use regex::Regex; use std::{ collections::BTreeMap, fs::{File, OpenOptions}, @@ -13,12 +11,6 @@ use std::{ pub use self::{msg_string::MsgString, plural_form::PluralForm}; -lazy_static! { - static ref APOSTROPHE_VARIATION: Regex = Regex::new("’").unwrap(); - static ref ESCAPED_DOUBLE_QUOTES: Regex = Regex::new(r#"\\""#).unwrap(); - static ref PARAMETERS: Regex = Regex::new(r"%\([^)]*\)").unwrap(); -} - /// A parsed gettext translation file. #[derive(Clone, Debug)] pub struct Translation { @@ -63,9 +55,6 @@ macro_rules! match_str { impl Translation { /// Load message entries from a gettext translation file. /// - /// The messages are normalized into a common format so that they can be compared to Android - /// string resource entries. - /// /// The only metadata that is parsed from the file is the "Plural-Form" header. It is assumed /// that the header value is one of some hard-coded values, so if new languages that have new /// plurals are added, the code will have to be updated. @@ -108,11 +97,11 @@ impl Translation { for line in lines { match_str! { (line.trim()) ["msgid \"", msg_id, "\""] => { - current_id = Some(normalize(msg_id)); + current_id = Some(msg_id.into()); } ["msgstr \"", translation, "\""] => { if let Some(id) = current_id.take() { - let value = MsgValue::Invariant(normalize(translation)); + let value = MsgValue::Invariant(MsgString::from_escaped(translation)); parsing_header = id.is_empty() && translation.is_empty(); @@ -123,7 +112,7 @@ impl Translation { current_plural_id = None; } ["msgid_plural \"", plural_id, "\""] => { - current_plural_id = Some(normalize(plural_id)); + current_plural_id = Some(MsgString::from_escaped(plural_id)); parsing_header = false; } ["msgstr[", plural_translation, "\""] => { @@ -137,7 +126,7 @@ impl Translation { let variant_msg = parse_line(&plural_translation[variant_id_end..], "] \"", "") .expect("Invalid plural msgstr"); - variants.insert(variant_id, normalize(variant_msg)); + variants.insert(variant_id, MsgString::from_escaped(variant_msg)); parsing_header = false; } ["\"", header, "\\n\""] => { @@ -241,14 +230,3 @@ fn parse_line<'l>(line: &'l str, prefix: &str, suffix: &str) -> Option<&'l str> None } } - -fn normalize(string: &str) -> MsgString { - // Use a single common apostrophe character - let string = APOSTROPHE_VARIATION.replace_all(&string, "'"); - // Mark where parameters are positioned, removing the parameter name - let string = PARAMETERS.replace_all(&string, "%"); - // Remove escaped double-quotes - let string = ESCAPED_DOUBLE_QUOTES.replace_all(&string, r#"""#); - - MsgString::from_escaped(string) -} diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs index bf8d0d8cf3..d11320b070 100644 --- a/android/translations-converter/src/main.rs +++ b/android/translations-converter/src/main.rs @@ -117,8 +117,8 @@ fn main() { for message in template { match message.value { - gettext::MsgValue::Invariant(_) => missing_translations.remove(&*message.id), - gettext::MsgValue::Plural { .. } => missing_plurals.remove(&*message.id), + gettext::MsgValue::Invariant(_) => missing_translations.remove(&message.id.normalize()), + gettext::MsgValue::Plural { .. } => missing_plurals.remove(&message.id.normalize()), }; } @@ -244,16 +244,16 @@ fn generate_translations( for translation in translations { match translation.value { gettext::MsgValue::Invariant(translation_value) => { - if let Some(android_key) = known_strings.remove(&*translation.id) { + if let Some(android_key) = known_strings.remove(&translation.id.normalize()) { localized_strings.push(android::StringResource::new( android_key, - &translation_value, + &translation_value.normalize(), )); } } gettext::MsgValue::Plural { values, .. } => { - if let Some(android_key) = known_plurals.remove(&*translation.id) { - let values = values.into_iter().map(|message| message.to_string()); + if let Some(android_key) = known_plurals.remove(&translation.id.normalize()) { + let values = values.into_iter().map(|message| message.normalize()); localized_plurals.push(android::PluralResource::new( android_key, |
