extract.py 3.8 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-2019 Juan Linietsky, Ariel Manzur.
  32. # Copyright (c) 2014-2019 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. def process_file(f, fname):
  45. global main_po, unique_str, unique_loc
  46. l = f.readline()
  47. lc = 1
  48. while (l):
  49. patterns = ['RTR(\"', 'TTR(\"']
  50. idx = 0
  51. pos = 0
  52. while (pos >= 0):
  53. pos = l.find(patterns[idx], pos)
  54. if (pos == -1):
  55. if (idx < len(patterns) - 1):
  56. idx += 1
  57. pos = 0
  58. continue
  59. pos += 5
  60. msg = ""
  61. while (pos < len(l) and (l[pos] != '"' or l[pos - 1] == '\\')):
  62. msg += l[pos]
  63. pos += 1
  64. location = os.path.relpath(fname).replace('\\', '/')
  65. if (line_nb):
  66. location += ":" + str(lc)
  67. if (not msg in unique_str):
  68. main_po += "\n#: " + location + "\n"
  69. main_po += 'msgid "' + msg + '"\n'
  70. main_po += 'msgstr ""\n'
  71. unique_str.append(msg)
  72. unique_loc[msg] = [location]
  73. elif (not location in unique_loc[msg]):
  74. # Add additional location to previous occurrence too
  75. msg_pos = main_po.find('\nmsgid "' + msg + '"')
  76. if (msg_pos == -1):
  77. print("Someone apparently thought writing Python was as easy as GDScript. Ping Akien.")
  78. main_po = main_po[:msg_pos] + ' ' + location + main_po[msg_pos:]
  79. unique_loc[msg].append(location)
  80. l = f.readline()
  81. lc += 1
  82. print("Updating the editor.pot template...")
  83. for fname in matches:
  84. with open(fname, "rb") as f:
  85. process_file(f, fname)
  86. with open("editor.pot", "wb") as f:
  87. f.write(main_po)
  88. if (os.name == "posix"):
  89. print("Wrapping template at 79 characters for compatibility with Weblate.")
  90. os.system("msgmerge -w79 editor.pot editor.pot > editor.pot.wrap")
  91. shutil.move("editor.pot.wrap", "editor.pot")
  92. shutil.move("editor.pot", "editor/translations/editor.pot")
  93. # TODO: Make that in a portable way, if we care; if not, kudos to Unix users
  94. if (os.name == "posix"):
  95. added = subprocess.check_output("git diff editor/translations/editor.pot | grep \+msgid | wc -l", shell=True)
  96. removed = subprocess.check_output("git diff editor/translations/editor.pot | grep \\\-msgid | wc -l", shell=True)
  97. print("\n# Template changes compared to the staged status:")
  98. print("# Additions: %s msgids.\n# Deletions: %s msgids." % (int(added), int(removed)))