extract.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/python
  2. import fnmatch
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. line_nb = False
  8. for arg in sys.argv[1:]:
  9. if (arg == "--with-line-nb"):
  10. print("Enabling line numbers in the context locations.")
  11. line_nb = True
  12. else:
  13. os.sys.exit("Non supported argument '" + arg + "'. Aborting.")
  14. if (not os.path.exists("editor")):
  15. os.sys.exit("ERROR: This script should be started from the root of the git repo.")
  16. matches = []
  17. for root, dirnames, filenames in os.walk('.'):
  18. for filename in fnmatch.filter(filenames, '*.cpp'):
  19. if (filename.find("collada") != -1):
  20. continue
  21. matches.append(os.path.join(root, filename))
  22. for filename in fnmatch.filter(filenames, '*.h'):
  23. if (filename.find("collada") != -1):
  24. continue
  25. matches.append(os.path.join(root, filename))
  26. matches.sort()
  27. unique_str = []
  28. unique_loc = {}
  29. main_po = """
  30. # LANGUAGE translation of the Godot Engine editor
  31. # Copyright (C) 2007-2017 Juan Linietsky, Ariel Manzur
  32. # Copyright (C) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)
  33. # This file is distributed under the same license as the Godot source code.
  34. #
  35. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
  36. #
  37. #, fuzzy
  38. msgid ""
  39. msgstr ""
  40. "Project-Id-Version: Godot Engine editor\\n"
  41. "Content-Type: text/plain; charset=UTF-8\\n"
  42. "Content-Transfer-Encoding: 8-bit\\n"
  43. """
  44. print("Updating the editor.pot template...")
  45. for fname in matches:
  46. f = open(fname, "rb")
  47. l = f.readline()
  48. lc = 1
  49. while (l):
  50. patterns = ['RTR(\"', 'TTR(\"']
  51. idx = 0
  52. pos = 0
  53. while (pos >= 0):
  54. pos = l.find(patterns[idx], pos)
  55. if (pos == -1):
  56. if (idx < len(patterns) - 1):
  57. idx += 1
  58. pos = 0
  59. continue
  60. pos += 5
  61. msg = ""
  62. while (pos < len(l) and (l[pos] != '"' or l[pos - 1] == '\\')):
  63. msg += l[pos]
  64. pos += 1
  65. location = os.path.relpath(fname).replace('\\', '/')
  66. if (line_nb):
  67. location += ":" + str(lc)
  68. if (not msg in unique_str):
  69. main_po += "\n#: " + location + "\n"
  70. main_po += 'msgid "' + msg + '"\n'
  71. main_po += 'msgstr ""\n'
  72. unique_str.append(msg)
  73. unique_loc[msg] = [location]
  74. elif (not location in unique_loc[msg]):
  75. # Add additional location to previous occurence too
  76. msg_pos = main_po.find('\nmsgid "' + msg + '"')
  77. if (msg_pos == -1):
  78. print("Someone apparently thought writing Python was as easy as GDScript. Ping Akien.")
  79. main_po = main_po[:msg_pos] + ' ' + location + main_po[msg_pos:]
  80. unique_loc[msg].append(location)
  81. l = f.readline()
  82. lc += 1
  83. f.close()
  84. f = open("editor.pot", "wb")
  85. f.write(main_po)
  86. f.close()
  87. if (os.name == "posix"):
  88. print("Wrapping template at 79 characters for compatibility with Weblate.")
  89. os.system("msgmerge -w79 editor.pot editor.pot > editor.pot.wrap")
  90. shutil.move("editor.pot.wrap", "editor.pot")
  91. shutil.move("editor.pot", "editor/translations/editor.pot")
  92. # TODO: Make that in a portable way, if we care; if not, kudos to Unix users
  93. if (os.name == "posix"):
  94. added = subprocess.check_output("git diff editor/translations/editor.pot | grep \+msgid | wc -l", shell=True)
  95. removed = subprocess.check_output("git diff editor/translations/editor.pot | grep \\\-msgid | wc -l", shell=True)
  96. print("\n# Template changes compared to the staged status:")
  97. print("# Additions: %s msgids.\n# Deletions: %s msgids." % (int(added), int(removed)))