diff options
author | Ghostkeeper <rubend@tutanota.com> | 2019-07-26 15:20:06 +0300 |
---|---|---|
committer | Ghostkeeper <rubend@tutanota.com> | 2019-07-26 15:20:06 +0300 |
commit | a3611404d62b2607c8f84599e99a4696c98f7d83 (patch) | |
tree | fa380ce71124903e23ebeea6f6fcd2e92bec6030 /scripts | |
parent | 3f7bd2ac23eef19d67e1e0bae70b45525dc72295 (diff) |
Find translations in source file
So it can add the translations to the destination file.
Contributes to issue CURA-6663.
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/lionbridge_import.py | 60 |
1 files changed, 59 insertions, 1 deletions
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.") |