extract_strings_from_XML.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 )
  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.encode( "utf-8" ) )
  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.encode( "utf-8" ) )
  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.encode( "utf-8" ) )
  41. else:
  42. if e.hasAttribute("text") and len(e.getAttribute("text")) > 0:
  43. # print "Label=", e.getAttribute("text"), " Comment=", comment
  44. line = ""
  45. if comment == None:
  46. line += "//I18N: " + file + "\n_(\"" + e.getAttribute("text") + "\")\n\n"
  47. else:
  48. line += "//I18N: " + file + "\n//I18N: " + comment + "\n_(\"" + e.getAttribute("text") + "\");\n\n"
  49. f.write( line.encode( "utf-8" ) )
  50. # don't recurse in children nodes for karts, they can contain sounds, etc. that should not be translated
  51. if not isKart:
  52. traverse(file, e, isChallenge, isGP, isKart, isTrack, isAchievements, level+1)
  53. filenames = sys.argv[1:]
  54. for file in filenames:
  55. #print "Parsing", file
  56. isChallenge = False
  57. isGP = False
  58. isKart = False
  59. isTrack = False
  60. isAchievements = False
  61. if file.endswith(".challenge"):
  62. isChallenge = True
  63. if file.endswith(".grandprix"):
  64. isGP = True
  65. if file.endswith("kart.xml"):
  66. isKart = True
  67. if file.endswith("track.xml"):
  68. isTrack = True
  69. if file.endswith("achievements.xml"):
  70. isAchievements = True
  71. try:
  72. doc = xml.dom.minidom.parse(file)
  73. except Exception as ex:
  74. print "============================================"
  75. print "/!\\ Expat doesn't like ", file, "! Error=", type(ex), " (", ex.args, ")"
  76. print "============================================"
  77. traverse(file, doc, isChallenge, isGP, isKart, isTrack, isAchievements)