fix_translation_memory.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ##% Script to fix a corrupted Translation memory from existing po files
  2. import os
  3. import re
  4. import argparse
  5. from pathlib import Path
  6. from fuzzywuzzy import fuzz
  7. from fuzzywuzzy import process
  8. import xml.etree.ElementTree as ET
  9. from xml.sax.saxutils import unescape, escape, quoteattr
  10. def load_existing_xmtm(path: Path) -> ET.Element:
  11. """Load existing xmtm file and return the root element"""
  12. tree = ET.parse(path)
  13. return tree.getroot()
  14. def load_existing_po(path: Path) -> dict:
  15. """Load existing po file and return a dictionary of msgid and msgstr"""
  16. content = path.read_text(encoding="utf-8")
  17. content = "".join(content.splitlines()[16:])
  18. return dict(re.findall(r'[^#]msgid.?\"+\s?([\s|\S]+?)\"*?msgstr.?\"([\s|\S]+?)\"?#', content))
  19. def sanitize(text: str) -> str:
  20. """Sanitize the text"""
  21. return unescape(text.replace("\"\"", "").replace("\"#~", ""))
  22. def main(tmx_source_path: Path, tmx_target_path: Path, i18n_path: Path):
  23. po_content = {}
  24. for file in i18n_path.rglob("cura.po"):
  25. print(os.path.join(i18n_path, file))
  26. po_content[file.relative_to(i18n_path).parts[0].replace("_", "-")] = load_existing_po(Path(os.path.join(i18n_path, file)))
  27. root = load_existing_xmtm(tmx_source_path)
  28. root_old = ET.ElementTree(root)
  29. ET.indent(root_old, ' ')
  30. root_old.write("old.tmx", encoding="utf-8", xml_declaration=True)
  31. for tu in root.iter("tu"):
  32. if "cura.pot" not in [t.text for t in tu.findall("prop") if t.attrib["type"] == "x-smartling-file"]:
  33. continue
  34. tuvs = tu.findall("tuv")
  35. key_source = tuvs[0].find("seg").text
  36. key_lang = tuvs[1].attrib["{http://www.w3.org/XML/1998/namespace}lang"]
  37. if key_lang in po_content and key_source in po_content[key_lang]:
  38. replaced_translation = po_content[key_lang][key_source]
  39. else:
  40. fuzz_match_ratio = [fuzz.ratio(k, key_source) for k in po_content[key_lang].keys()]
  41. fuzz_max_ratio = max(fuzz_match_ratio)
  42. fuzz_match_key = list(po_content[key_lang].keys())[fuzz_match_ratio.index(fuzz_max_ratio)]
  43. if fuzz_max_ratio > 90:
  44. replaced_translation = po_content[key_lang][fuzz_match_key]
  45. tuvs[0].find("seg").text = sanitize(fuzz_match_key)
  46. else:
  47. print(f"[{key_lang}] {key_source} == {fuzz_match_key} [{fuzz_max_ratio}]")
  48. continue
  49. tuvs[1].find("seg").text = sanitize(replaced_translation)
  50. fixed_root = ET.ElementTree(root)
  51. ET.indent(fixed_root, ' ')
  52. fixed_root.write(tmx_target_path, encoding="utf-8", xml_declaration=True)
  53. if __name__ == "__main__":
  54. parser = argparse.ArgumentParser(description="Fix a corrupted Translation memory from existing po files")
  55. parser.add_argument("tmx_source_path", type=Path, help="Path to the source TMX file")
  56. parser.add_argument("tmx_target_path", type=Path, help="Path to the target TMX file")
  57. parser.add_argument("i18n_path", type=Path, help="Path to the i18n folder")
  58. args = parser.parse_args()
  59. main(args.tmx_source_path, args.tmx_target_path, args.i18n_path)