summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDavid Göransson <david.goransson@mullvad.net>2024-08-07 18:44:12 +0200
committerAlbin <albin@mullvad.net>2024-08-21 11:52:30 +0200
commit462457efea3493653bce4ea64a7db77b29265a67 (patch)
treea2087478671f478c85c2e6861aa81a7a8ec244b3
parent26e03f20c8ee93303cd9afb2432601651571dc05 (diff)
downloadmullvadvpn-462457efea3493653bce4ea64a7db77b29265a67.tar.xz
mullvadvpn-462457efea3493653bce4ea64a7db77b29265a67.zip
Support generating relay locations
-rw-r--r--android/translations-converter/src/main.rs113
1 files changed, 108 insertions, 5 deletions
diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs
index afb760f899..7a340d7483 100644
--- a/android/translations-converter/src/main.rs
+++ b/android/translations-converter/src/main.rs
@@ -35,6 +35,7 @@ mod android;
mod gettext;
mod normalize;
+use crate::gettext::MsgValue;
use crate::normalize::Normalize;
use std::{
collections::HashMap,
@@ -121,8 +122,8 @@ fn main() {
for message in template {
match message.value {
- gettext::MsgValue::Invariant(_) => missing_translations.remove(&message.id.normalize()),
- gettext::MsgValue::Plural { .. } => missing_plurals.remove(&message.id.normalize()),
+ MsgValue::Invariant(_) => missing_translations.remove(&message.id.normalize()),
+ MsgValue::Plural { .. } => missing_plurals.remove(&message.id.normalize()),
};
}
@@ -182,7 +183,7 @@ fn main() {
gettext::MsgEntry {
id,
- value: gettext::MsgValue::Plural {
+ value: MsgValue::Plural {
plural_id,
values: vec![gettext::MsgString::empty(), gettext::MsgString::empty()],
},
@@ -191,6 +192,65 @@ fn main() {
)
.expect("Failed to append missing plural translations to message template file");
}
+
+ // Generate all relay locale files
+
+ let relay_template_path = locale_dir.join("relay-locations.pot");
+
+ let default_translations = gettext::Messages::from_file(&relay_template_path)
+ .expect("Failed to load translations for a locale");
+
+ let resources_dir = Path::new("../lib/resource/src/main/res");
+
+ let relay_locations_path = resources_dir.join("xml/relay_locations.xml");
+
+ let mut localized_strings = android::StringResources::new();
+ for translation in default_translations {
+ match translation.value {
+ MsgValue::Invariant(_) => {
+ if !translation.id.is_empty() {
+ localized_strings.push(android::StringResource::new(
+ translation.id.normalize(),
+ &translation.id.normalize(),
+ ));
+ }
+ }
+ MsgValue::Plural { .. } => {}
+ }
+ }
+
+ localized_strings.sort();
+
+ fs::write(relay_locations_path, localized_strings.to_string())
+ .expect("Failed to create Android locale file");
+
+ let relay_locale_files = fs::read_dir(locale_dir)
+ .expect("Failed to open root locale directory")
+ .filter_map(|dir_entry_result| dir_entry_result.ok())
+ .map(|dir_entry| dir_entry.path())
+ .filter(|dir_entry_path| dir_entry_path.is_dir())
+ .map(|dir_path| dir_path.join("relay-locations.po"))
+ .filter(|file_path| file_path.exists());
+
+ for relay_file in relay_locale_files {
+ let locale = relay_file
+ .parent()
+ .unwrap()
+ .file_name()
+ .unwrap()
+ .to_str()
+ .unwrap();
+ let destination_dir = resources_dir.join(android_xml_directory(locale));
+
+ if !destination_dir.exists() {
+ fs::create_dir(&destination_dir).expect("Failed to create Android locale directory");
+ }
+
+ let translations = gettext::Messages::from_file(&relay_file)
+ .expect("Failed to load translations for a locale");
+
+ generate_relay_translations(translations, destination_dir.join("relay_locations.xml"));
+ }
}
/// Determines the localized value resources directory name based on a locale specification.
@@ -211,6 +271,49 @@ fn android_locale_directory(locale: &str) -> String {
directory
}
+/// Determines the localized value resources directory name based on a locale specification.
+///
+/// This just makes sure a locale such as `en-US' gets correctly mapped to the directory name
+/// `xml-en-rUS`.
+fn android_xml_directory(locale: &str) -> String {
+ let mut directory = String::from("xml-");
+ let mut parts = locale.split('-');
+
+ directory.push_str(parts.next().unwrap());
+
+ if let Some(region) = parts.next() {
+ directory.push_str("-r");
+ directory.push_str(region);
+ }
+
+ directory
+}
+
+/// Generate translated Android relay resource strings for a locale.
+fn generate_relay_translations(
+ translations: gettext::Messages,
+ strings_output_path: impl AsRef<Path>,
+) {
+ let mut localized_strings = android::StringResources::new();
+
+ for translation in translations {
+ match translation.value {
+ MsgValue::Invariant(translation_value) => {
+ localized_strings.push(android::StringResource::new(
+ translation.id.normalize(),
+ &translation_value.normalize(),
+ ));
+ }
+ MsgValue::Plural { .. } => {}
+ }
+ }
+
+ localized_strings.sort();
+
+ fs::write(strings_output_path, localized_strings.to_string())
+ .expect("Failed to create Android 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
@@ -238,7 +341,7 @@ fn generate_translations(
for translation in translations {
match translation.value {
- gettext::MsgValue::Invariant(translation_value) => {
+ MsgValue::Invariant(translation_value) => {
if let Some(android_key) = known_strings.remove(&translation.id.normalize()) {
localized_strings.push(android::StringResource::new(
android_key,
@@ -246,7 +349,7 @@ fn generate_translations(
));
}
}
- gettext::MsgValue::Plural { values, .. } => {
+ MsgValue::Plural { values, .. } => {
if let Some(android_key) = known_plurals.remove(&translation.id.normalize()) {
let values = values.into_iter().map(|message| message.normalize());