From a3611404d62b2607c8f84599e99a4696c98f7d83 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Fri, 26 Jul 2019 14:20:06 +0200 Subject: Find translations in source file So it can add the translations to the destination file. Contributes to issue CURA-6663. --- scripts/lionbridge_import.py | 60 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'scripts') diff --git a/scripts/lionbridge_import.py b/scripts/lionbridge_import.py index f2f505e52f..2ee9c0a937 100644 --- a/scripts/lionbridge_import.py +++ b/scripts/lionbridge_import.py @@ -59,7 +59,65 @@ def destination_uranium() -> str: return os.path.abspath(os.path.join(UM.__file__, "..", "..", "resources", "i18n")) def merge(source: str, destination: str) -> str: - return "TODO" + last_destination = { + "msgctxt": "", + "msgid": "", + "msgstr": "" + } + + current_state = "none" + for line in destination.split("\n"): + if line.startswith("msgctxt \""): + current_state = "msgctxt" + line = line[8:] + last_destination[current_state] = "" + elif line.startswith("msgid \""): + current_state = "msgid" + line = line[6:] + last_destination[current_state] = "" + elif line.startswith("msgstr \""): + current_state = "msgstr" + line = line[7:] + last_destination[current_state] = "" + + if line.startswith("\"") and line.endswith("\""): + last_destination[current_state] += line[1:-1] + else: #White lines trigger us to search for the translation in the source file. + if last_destination["msgstr"] == "" and last_destination["msgid"] != "": #No translation for this yet! + translation = find_translation(source, last_destination["msgctxt"], last_destination["msgid"]) + +def find_translation(source: str, msgctxt, msgid): + last_source = { + "msgctxt": "", + "msgid": "", + "msgstr": "" + } + + current_state = "none" + for line in source.split("\n"): + if line.startswith("msgctxt \""): + current_state = "msgctxt" + line = line[8:] + last_source[current_state] = "" + elif line.startswith("msgid \""): + current_state = "msgid" + line = line[6:] + last_source[current_state] = "" + elif line.startswith("msgstr \""): + current_state = "msgstr" + line = line[7:] + last_source[current_state] = "" + + if line.startswith("\"") and line.endswith("\""): + last_source[current_state] += line[1:-1] + else: #White lines trigger us to process this translation. Is it the correct one? + if last_source["msgctxt"] == msgctxt and last_source["msgid"] == msgid: + if last_source["msgstr"] == "": + print("!!! Empty translation for {" + msgctxt + "}", msgid, "!!!") + return last_source["msgstr"] + + #Still here? Then the entire msgctxt+msgid combination was not found at all. + print("!!! Missing translation for {" + msgctxt + "}", msgid, "!!!") if __name__ == "__main__": argparser = argparse.ArgumentParser(description = "Import translation files from Lionbridge.") -- cgit v1.2.3