diff options
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/src/main.rs | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs index 6e02771c69..0c4d1a4896 100644 --- a/android/translations-converter/src/main.rs +++ b/android/translations-converter/src/main.rs @@ -3,7 +3,8 @@ //! 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. +//! string value, the translated string is used in the new Android strings file for the respective +//! locale. //! //! 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 @@ -16,10 +17,12 @@ use regex::Regex; use std::{ collections::HashMap, fs::{self, File}, + path::Path, }; fn main() { - let strings_file = File::open("../src/main/res/values/strings.xml") + let resources_dir = Path::new("../src/main/res"); + let strings_file = File::open(resources_dir.join("values/strings.xml")) .expect("Failed to open string resources file"); let string_resources: android::StringResources = serde_xml_rs::from_reader(strings_file).expect("Failed to read string resources file"); @@ -44,18 +47,54 @@ fn main() { .filter(|file_path| file_path.exists()); for locale_file in locale_files { - generate_translations(&known_strings, gettext::load_file(&locale_file)); + let locale = locale_file + .parent() + .unwrap() + .file_name() + .unwrap() + .to_str() + .unwrap(); + let destination_dir = resources_dir.join(&android_locale_directory(locale)); + + if !destination_dir.exists() { + fs::create_dir(&destination_dir).expect("Failed to create Android locale directory"); + } + + generate_translations( + &known_strings, + gettext::load_file(&locale_file), + destination_dir.join("strings.xml"), + ); + } +} + +/// 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 +/// `values-en-rUS`. +fn android_locale_directory(locale: &str) -> String { + let mut directory = String::from("values-"); + 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 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. +/// translation. An Android string resource XML file is created with the translated strings. fn generate_translations( known_strings: &HashMap<String, String>, translations: Vec<gettext::MsgEntry>, + output_path: impl AsRef<Path>, ) { let mut localized_resource = android::StringResources::new(); @@ -68,5 +107,6 @@ fn generate_translations( } } - dbg!(localized_resource); + fs::write(output_path, localized_resource.to_string()) + .expect("Failed to create Android locale file"); } |
