mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-10 16:27:51 -06:00
Write resulting file
It seems to fail now for plurals. I didn't think of that yet. Contributes to issue CURA-6663.
This commit is contained in:
parent
58c32f97a1
commit
23826d3404
1 changed files with 28 additions and 13 deletions
|
@ -39,7 +39,8 @@ def lionbridge_import(source: str) -> None:
|
||||||
with open(destination_file) as f:
|
with open(destination_file) as f:
|
||||||
destination_str = f.read()
|
destination_str = f.read()
|
||||||
result = merge(source_str, destination_str)
|
result = merge(source_str, destination_str)
|
||||||
print(result) #DEBUG! Instead we should write this to a file.
|
with open(destination_file, "w") as f:
|
||||||
|
f.write(result)
|
||||||
|
|
||||||
## Gets the destination path to copy the translations for Cura to.
|
## Gets the destination path to copy the translations for Cura to.
|
||||||
# \return Destination path for Cura.
|
# \return Destination path for Cura.
|
||||||
|
@ -65,10 +66,11 @@ def destination_uranium() -> str:
|
||||||
# \param source The contents of the source .po file.
|
# \param source The contents of the source .po file.
|
||||||
# \param destination The contents of the destination .po file.
|
# \param destination The contents of the destination .po file.
|
||||||
def merge(source: str, destination: str) -> str:
|
def merge(source: str, destination: str) -> str:
|
||||||
|
result_lines = []
|
||||||
last_destination = {
|
last_destination = {
|
||||||
"msgctxt": "",
|
"msgctxt": "\"\"\n",
|
||||||
"msgid": "",
|
"msgid": "\"\"\n",
|
||||||
"msgstr": ""
|
"msgstr": "\"\"\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
current_state = "none"
|
current_state = "none"
|
||||||
|
@ -87,10 +89,22 @@ def merge(source: str, destination: str) -> str:
|
||||||
last_destination[current_state] = ""
|
last_destination[current_state] = ""
|
||||||
|
|
||||||
if line.startswith("\"") and line.endswith("\""):
|
if line.startswith("\"") and line.endswith("\""):
|
||||||
last_destination[current_state] += line[1:-1]
|
last_destination[current_state] += line + "\n"
|
||||||
else: #White lines trigger us to search for the translation in the source file.
|
else: #White lines or comment 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!
|
if last_destination["msgstr"] == "\"\"\n" and last_destination["msgid"] != "\"\"\n": #No translation for this yet!
|
||||||
translation = find_translation(source, last_destination["msgctxt"], last_destination["msgid"])
|
last_destination["msgstr"] = find_translation(source, last_destination["msgctxt"], last_destination["msgid"]) #Actually place the translation in.
|
||||||
|
if last_destination["msgctxt"] != "\"\"\n" or last_destination["msgid"] != "\"\"\n" or last_destination["msgstr"] != "\"\"\n":
|
||||||
|
result_lines.append("msgctxt {msgctxt}".format(msgctxt = last_destination["msgctxt"][:-1])) #The [:-1] to strip the last newline.
|
||||||
|
result_lines.append("msgid {msgid}".format(msgid = last_destination["msgid"][:-1]))
|
||||||
|
result_lines.append("msgstr {msgstr}".format(msgstr = last_destination["msgstr"][:-1]))
|
||||||
|
last_destination = {
|
||||||
|
"msgctxt": "\"\"\n",
|
||||||
|
"msgid": "\"\"\n",
|
||||||
|
"msgstr": "\"\"\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
result_lines.append(line) #This line itself.
|
||||||
|
return "\n".join(result_lines)
|
||||||
|
|
||||||
## Finds a translation in the source file.
|
## Finds a translation in the source file.
|
||||||
# \param source The contents of the source .po file.
|
# \param source The contents of the source .po file.
|
||||||
|
@ -98,9 +112,9 @@ def merge(source: str, destination: str) -> str:
|
||||||
# \param msgid The id of the translation to find.
|
# \param msgid The id of the translation to find.
|
||||||
def find_translation(source: str, msgctxt: str, msgid: str) -> str:
|
def find_translation(source: str, msgctxt: str, msgid: str) -> str:
|
||||||
last_source = {
|
last_source = {
|
||||||
"msgctxt": "",
|
"msgctxt": "\"\"\n",
|
||||||
"msgid": "",
|
"msgid": "\"\"\n",
|
||||||
"msgstr": ""
|
"msgstr": "\"\"\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
current_state = "none"
|
current_state = "none"
|
||||||
|
@ -119,15 +133,16 @@ def find_translation(source: str, msgctxt: str, msgid: str) -> str:
|
||||||
last_source[current_state] = ""
|
last_source[current_state] = ""
|
||||||
|
|
||||||
if line.startswith("\"") and line.endswith("\""):
|
if line.startswith("\"") and line.endswith("\""):
|
||||||
last_source[current_state] += line[1:-1]
|
last_source[current_state] += line + "\n"
|
||||||
else: #White lines trigger us to process this translation. Is it the correct one?
|
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["msgctxt"] == msgctxt and last_source["msgid"] == msgid:
|
||||||
if last_source["msgstr"] == "":
|
if last_source["msgstr"] == "\"\"\n":
|
||||||
print("!!! Empty translation for {" + msgctxt + "}", msgid, "!!!")
|
print("!!! Empty translation for {" + msgctxt + "}", msgid, "!!!")
|
||||||
return last_source["msgstr"]
|
return last_source["msgstr"]
|
||||||
|
|
||||||
#Still here? Then the entire msgctxt+msgid combination was not found at all.
|
#Still here? Then the entire msgctxt+msgid combination was not found at all.
|
||||||
print("!!! Missing translation for {" + msgctxt + "}", msgid, "!!!")
|
print("!!! Missing translation for {" + msgctxt + "}", msgid, "!!!")
|
||||||
|
return "\"\"\n"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
argparser = argparse.ArgumentParser(description = "Import translation files from Lionbridge.")
|
argparser = argparse.ArgumentParser(description = "Import translation files from Lionbridge.")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue