diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-07-21 00:44:26 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-08-11 22:59:38 +0000 |
| commit | b08747cc1939ac7c79a4c65289988be61094aa28 (patch) | |
| tree | 70e4a47131f6fcd0f9203e7cb11c4f0ee35edb4e /android | |
| parent | 5e115bb29295faf2e4d8fc619059279cee2fa628 (diff) | |
| download | mullvadvpn-b08747cc1939ac7c79a4c65289988be61094aa28.tar.xz mullvadvpn-b08747cc1939ac7c79a4c65289988be61094aa28.zip | |
Localize URLs when possible
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/main.rs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs index 487f86122c..aef9a8c6e3 100644 --- a/android/translations-converter/src/main.rs +++ b/android/translations-converter/src/main.rs @@ -38,14 +38,14 @@ fn main() { string_resources.normalize(); - let known_strings: HashMap<_, _> = string_resources + let (known_urls, known_strings): (HashMap<_, _>, _) = string_resources .into_iter() .map(|string| { let android_id = string.name; (string.value, android_id) }) - .collect(); + .partition(|(string_value, _)| string_value.starts_with("https://mullvad.net/en/")); let locale_files = fs::read_dir("../../gui/locales") .expect("Failed to open root locale directory") @@ -69,6 +69,8 @@ fn main() { } generate_translations( + locale, + known_urls.clone(), known_strings.clone(), gettext::load_file(&locale_file), destination_dir.join("strings.xml"), @@ -99,7 +101,13 @@ fn android_locale_directory(locale: &str) -> String { /// 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. An Android string resource XML file is created with the translated strings. +/// +/// URL strings are treated differently. The "translated" URLs have a locale specified in them. If +/// mapping from the translation locale to a website locale fails, the "translated" URL is not +/// generated, and the app falls back to the original URL value with the english locale. fn generate_translations( + locale: &str, + known_urls: HashMap<String, String>, mut known_strings: HashMap<String, String>, translations: Vec<gettext::MsgEntry>, output_path: impl AsRef<Path>, @@ -115,6 +123,17 @@ fn generate_translations( } } + if let Some(web_locale) = website_locale(locale) { + let locale_path = format!("/{}/", web_locale); + + for (url, android_key) in known_urls { + localized_resource.push(android::StringResource::new( + android_key, + &url.replacen("/en/", &locale_path, 1), + )); + } + } + fs::write(output_path, localized_resource.to_string()) .expect("Failed to create Android locale file"); @@ -124,3 +143,18 @@ fn generate_translations( println!(" {}: {}", id, missing_translation); } } + +/// Tries to map a translation locale to a locale used on the Mullvad website. +/// +/// The mapping is trivial if no region is specified. Otherwise the region code must be manually +/// converted. +fn website_locale(locale: &str) -> Option<&str> { + match locale { + locale if !locale.contains("-") => Some(locale), + "zh-TW" => Some("zh-hant"), + unknown_locale => { + eprintln!("Unknown locale: {}", unknown_locale); + None + } + } +} |
