convert_txt_ts.py 709 B

123456789101112131415161718192021222324252627
  1. import xml.etree.ElementTree as ET
  2. import sys
  3. inFile = sys.argv[1]
  4. outFile = inFile + ".ts"
  5. sourceFile = inFile[:-3] + "ts"
  6. tree = ET.parse(sourceFile)
  7. txt = open(inFile, encoding="utf-8").read()
  8. txt = txt.split("\n")
  9. tranIndex = 0
  10. root = tree.getroot()
  11. for page in root:
  12. for message in page:
  13. if message.tag == "name":
  14. pass
  15. else:
  16. for item in message:
  17. if item.tag == "translation":
  18. t = txt[tranIndex].replace(r"\n", "\n") # \n转换行
  19. item.text = t
  20. # if item.attrib.get("type"):
  21. # item.attrib.pop("type")
  22. tranIndex += 1
  23. tree.write(outFile)