extract_strings_from_XML.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import xml.dom.minidom
  2. import sys
  3. import codecs
  4. f = open('./data/po/gui_strings.h', 'w')
  5. f.write(codecs.BOM_UTF8.decode('utf-8'))
  6. def traverse(file, node, isChallenge, isGP, isKart, isTrack, isAchievements, level=0):
  7. for e in node.childNodes:
  8. if e.localName == None:
  9. continue
  10. #print ' '*level, e.localName
  11. comment = None
  12. if e.hasAttribute("I18N"):
  13. comment = e.getAttribute("I18N")
  14. if e.localName == "subtitle" and e.hasAttribute("text") and len(e.getAttribute("text")) > 0:
  15. #print "Label=", e.getAttribute("name"), " Comment=", comment
  16. line = ""
  17. if comment == None:
  18. line += "//I18N: Cutscene subtitle from " + file + "\n_(\"" + e.getAttribute("text") + "\")\n\n"
  19. else:
  20. line += "//I18N: Cutscene subtitle from " + file + "\n//I18N: " + comment + "\n_(\"" + e.getAttribute("text") + "\");\n\n"
  21. f.write(line)
  22. if isChallenge or isGP or isKart or isTrack or isAchievements:
  23. if isTrack and e.hasAttribute("internal") and e.getAttribute("internal") == "Y": continue
  24. if e.hasAttribute("name") and len(e.getAttribute("name")) > 0:
  25. #print "Label=", e.getAttribute("name"), " Comment=", comment
  26. line = ""
  27. if comment == None:
  28. line += "//I18N: " + file + "\n_(\"" + e.getAttribute("name") + "\")\n\n"
  29. else:
  30. line += "//I18N: File : " + file + "\n//I18N: " + comment + "\n_(\"" + e.getAttribute("name") + "\");\n\n"
  31. f.write(line)
  32. # challenges and GPs can have a description file; karts don't
  33. if e.hasAttribute("description") and len(e.getAttribute("description")) > 0:
  34. # print "Label=", e.getAttribute("description"), " Comment=", comment
  35. line = ""
  36. if comment == None:
  37. line += "//I18N: " + file + "\n_(\"" + e.getAttribute("description") + "\")\n\n"
  38. else:
  39. line += "//I18N: File : " + file + "\n//I18N: " + comment + "\n_(\"" + e.getAttribute("description") + "\");\n\n"
  40. f.write(line)
  41. else:
  42. if e.nodeName == "string" and e.hasAttribute("name") and e.getAttribute("name").startswith("po_") and e.firstChild is not None:
  43. line = "//I18N: In Android UI, " + e.getAttribute("name") + "\n_(\"" + e.firstChild.nodeValue + "\")\n\n"
  44. f.write(line)
  45. elif e.hasAttribute("text") and len(e.getAttribute("text")) > 0:
  46. # print "Label=", e.getAttribute("text"), " Comment=", comment
  47. line = ""
  48. if comment == None:
  49. line += "//I18N: " + file + "\n_(\"" + e.getAttribute("text").replace("\"", "\\\"") + "\")\n\n"
  50. else:
  51. line += "//I18N: " + file + "\n//I18N: " + comment + "\n_(\"" + e.getAttribute("text") + "\");\n\n"
  52. f.write(line)
  53. # don't recurse in children nodes for karts, they can contain sounds, etc. that should not be translated
  54. if not isKart:
  55. traverse(file, e, isChallenge, isGP, isKart, isTrack, isAchievements, level+1)
  56. filenames = sys.argv[1:]
  57. for file in filenames:
  58. #print "Parsing", file
  59. isChallenge = False
  60. isGP = False
  61. isKart = False
  62. isTrack = False
  63. isAchievements = False
  64. if file.endswith(".challenge"):
  65. isChallenge = True
  66. if file.endswith(".grandprix"):
  67. isGP = True
  68. if file.endswith("kart.xml"):
  69. isKart = True
  70. if file.endswith("track.xml"):
  71. isTrack = True
  72. if file.endswith("achievements.xml"):
  73. isAchievements = True
  74. try:
  75. doc = xml.dom.minidom.parse(file)
  76. except Exception as ex:
  77. print("============================================")
  78. print("/!\\ Expat doesn't like ", file, "! Error=", type(ex), " (", ex.args, ")")
  79. print("============================================")
  80. traverse(file, doc, isChallenge, isGP, isKart, isTrack, isAchievements)