diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-07-13 21:43:01 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-08-11 22:59:38 +0000 |
| commit | 1595af8a77c2574aac466150031d5f1ed865fd27 (patch) | |
| tree | ca85c12597d7eab5b74df47765bcca0f12476520 /android | |
| parent | f833d26e5a14519bfd363d5c79f3227c4419f5d1 (diff) | |
| download | mullvadvpn-1595af8a77c2574aac466150031d5f1ed865fd27.tar.xz mullvadvpn-1595af8a77c2574aac466150031d5f1ed865fd27.zip | |
Handle apostrophe variation
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/gettext.rs | 22 | ||||
| -rw-r--r-- | android/translations-converter/src/main.rs | 3 |
2 files changed, 20 insertions, 5 deletions
diff --git a/android/translations-converter/src/gettext.rs b/android/translations-converter/src/gettext.rs index bbec6bb798..8f10ff6f8d 100644 --- a/android/translations-converter/src/gettext.rs +++ b/android/translations-converter/src/gettext.rs @@ -1,9 +1,15 @@ +use lazy_static::lazy_static; +use regex::Regex; use std::{ fs::File, io::{BufRead, BufReader}, path::Path, }; +lazy_static! { + static ref APOSTROPHE_VARIATION: Regex = Regex::new("’").unwrap(); +} + /// A message entry in a gettext translation file. #[derive(Clone, Debug)] pub struct MsgEntry { @@ -12,6 +18,9 @@ pub struct MsgEntry { } /// 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. pub fn load_file(file_path: impl AsRef<Path>) -> Vec<MsgEntry> { let mut entries = Vec::new(); let mut current_id = None; @@ -22,9 +31,9 @@ pub fn load_file(file_path: impl AsRef<Path>) -> Vec<MsgEntry> { let line = line.trim(); if let Some(msg_id) = parse_line(line, "msgid \"", "\"") { - current_id = Some(msg_id); + current_id = Some(normalize(msg_id)); } else { - if let Some(value) = parse_line(line, "msgstr \"", "\"") { + if let Some(value) = parse_line(line, "msgstr \"", "\"").map(String::from) { if let Some(id) = current_id.take() { entries.push(MsgEntry { id, value }); } @@ -37,13 +46,18 @@ pub fn load_file(file_path: impl AsRef<Path>) -> Vec<MsgEntry> { entries } -fn parse_line(line: &str, prefix: &str, suffix: &str) -> Option<String> { +fn parse_line<'l>(line: &'l str, prefix: &str, suffix: &str) -> Option<&'l str> { if line.starts_with(prefix) && line.ends_with(suffix) { let start = prefix.len(); let end = line.len() - suffix.len(); - Some(line[start..end].to_owned()) + Some(&line[start..end]) } else { None } } + +fn normalize(string: &str) -> String { + // Use a single common apostrophe character + APOSTROPHE_VARIATION.replace_all(&string, "'").into_owned() +} diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs index 24bf06a574..0ec1b2e34f 100644 --- a/android/translations-converter/src/main.rs +++ b/android/translations-converter/src/main.rs @@ -8,7 +8,8 @@ //! //! To make the comparison work on most strings, the Android and gettext messages are normalized //! first. This means that new lines in the XML files are removed and collapsed into a single space -//! and apostrophes are unescaped. +//! and there is also a small workaround for having different apostrophe characters in the GUI in +//! some messages. //! //! Note that this conversion procedure is very raw and likely very brittle, so while it works for //! most cases, it is important to keep in mind that this is just a helper tool and manual steps are |
