summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-07-28 03:38:34 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-08-11 22:59:38 +0000
commit9fed012acf3ff27e4a1028911a27daa74222d7d8 (patch)
treea1a942bb20d32838cf726f5cebbd71bebc7dd7af
parent8a76c5e7a83b0d91e039c636d4b2837f8f95f323 (diff)
downloadmullvadvpn-9fed012acf3ff27e4a1028911a27daa74222d7d8.tar.xz
mullvadvpn-9fed012acf3ff27e4a1028911a27daa74222d7d8.zip
Append missing translations to the template file
-rw-r--r--android/translations-converter/src/gettext.rs26
-rw-r--r--android/translations-converter/src/main.rs21
2 files changed, 40 insertions, 7 deletions
diff --git a/android/translations-converter/src/gettext.rs b/android/translations-converter/src/gettext.rs
index b4b241502d..edd5fbd538 100644
--- a/android/translations-converter/src/gettext.rs
+++ b/android/translations-converter/src/gettext.rs
@@ -1,8 +1,8 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::{
- fs::File,
- io::{BufRead, BufReader},
+ fs::{File, OpenOptions},
+ io::{self, BufRead, BufReader, BufWriter, Write},
path::Path,
};
@@ -49,6 +49,28 @@ pub fn load_file(file_path: impl AsRef<Path>) -> Vec<MsgEntry> {
entries
}
+/// Append message entries to a translation file.
+///
+/// This is used to append missing translation entries back to the base translation template file.
+pub fn append_to_template(
+ file_path: impl AsRef<Path>,
+ entries: impl Iterator<Item = MsgEntry>,
+) -> Result<(), io::Error> {
+ let file = OpenOptions::new()
+ .write(true)
+ .append(true)
+ .open(file_path)?;
+ let mut writer = BufWriter::new(file);
+
+ for entry in entries {
+ writeln!(writer)?;
+ writeln!(writer, "msgid {:?}", entry.id)?;
+ writeln!(writer, "msgstr {:?}", entry.value)?;
+ }
+
+ Ok(())
+}
+
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();
diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs
index 51afed1b67..56ca35a910 100644
--- a/android/translations-converter/src/main.rs
+++ b/android/translations-converter/src/main.rs
@@ -49,7 +49,8 @@ fn main() {
let mut missing_translations = known_strings.clone();
- let locale_files = fs::read_dir("../../gui/locales")
+ let locale_dir = Path::new("../../gui/locales");
+ let 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())
@@ -80,11 +81,21 @@ fn main() {
);
}
- println!("Missing translations:");
-
- for (missing_translation, id) in missing_translations {
- println!(" {}: {}", id, missing_translation);
+ if !missing_translations.is_empty() {
+ println!("Appending missing translations to template file:");
}
+
+ gettext::append_to_template(
+ locale_dir.join("messages.pot"),
+ missing_translations
+ .into_iter()
+ .inspect(|(missing_translation, id)| println!(" {}: {}", id, missing_translation))
+ .map(|(id, _)| gettext::MsgEntry {
+ id,
+ value: String::new(),
+ }),
+ )
+ .expect("Failed to append missing translations to message template file");
}
/// Determines the localized value resources directory name based on a locale specification.