summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-07-10 20:45:07 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-08-11 22:06:54 +0000
commit6a03c2c26dfa90806983dd086109ccf69b3db7ac (patch)
treee425b448d07da11d4711b146fb0112b8608283be
parent5ff21d92bc25102e770547ed29f88a5521a45c26 (diff)
downloadmullvadvpn-6a03c2c26dfa90806983dd086109ccf69b3db7ac.tar.xz
mullvadvpn-6a03c2c26dfa90806983dd086109ccf69b3db7ac.zip
Generate translations with known strings
-rw-r--r--android/translations-converter/src/android.rs24
-rw-r--r--android/translations-converter/src/main.rs34
2 files changed, 57 insertions, 1 deletions
diff --git a/android/translations-converter/src/android.rs b/android/translations-converter/src/android.rs
index 2d1c710e9f..e74562324a 100644
--- a/android/translations-converter/src/android.rs
+++ b/android/translations-converter/src/android.rs
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
+use std::ops::{Deref, DerefMut};
/// Contents of an Android string resources file.
///
@@ -20,6 +21,29 @@ pub struct StringResource {
pub value: String,
}
+impl StringResources {
+ /// Create an empty list of Android string resources.
+ pub fn new() -> Self {
+ StringResources {
+ entries: Vec::new(),
+ }
+ }
+}
+
+impl Deref for StringResources {
+ type Target = Vec<StringResource>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.entries
+ }
+}
+
+impl DerefMut for StringResources {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.entries
+ }
+}
+
impl IntoIterator for StringResources {
type Item = StringResource;
type IntoIter = std::vec::IntoIter<Self::Item>;
diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs
index c38d903829..6e02771c69 100644
--- a/android/translations-converter/src/main.rs
+++ b/android/translations-converter/src/main.rs
@@ -1,4 +1,13 @@
//! Helper tool to convert translations from gettext messages to Android string resources.
+//!
+//! The procedure for converting the translations is relatively simple. The base Android string
+//! resources file is first loaded, and then each gettext translation file is loaded and compared to
+//! the Android base strings. For every translation string that matches exactly the Android base
+//! string value, the translated string is used.
+//!
+//! 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
+//! likely to be needed from time to time.
mod android;
mod gettext;
@@ -35,6 +44,29 @@ fn main() {
.filter(|file_path| file_path.exists());
for locale_file in locale_files {
- dbg!(gettext::load_file(locale_file));
+ generate_translations(&known_strings, gettext::load_file(&locale_file));
}
}
+
+/// Generate translated Android resource strings for a locale.
+///
+/// Based on the gettext translated message entries, it finds the messages with message IDs that
+/// match known Android string resource values, and obtains the string resource ID for the
+/// translation.
+fn generate_translations(
+ known_strings: &HashMap<String, String>,
+ translations: Vec<gettext::MsgEntry>,
+) {
+ let mut localized_resource = android::StringResources::new();
+
+ for translation in translations {
+ if let Some(android_key) = known_strings.get(&translation.id) {
+ localized_resource.push(android::StringResource {
+ name: android_key.clone(),
+ value: translation.value,
+ });
+ }
+ }
+
+ dbg!(localized_resource);
+}