manage_translations.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/usr/bin/env python
  2. #
  3. # NOTE: This script is based on django's manage_translations.py script
  4. # (https://github.com/django/django/blob/master/scripts/manage_translations.py)
  5. #
  6. # This python file contains utility scripts to manage taiga translations.
  7. # It has to be run inside the taiga-back git root directory.
  8. #
  9. # The following commands are available:
  10. #
  11. # * update_catalogs: check for new strings in taiga-back catalogs, and
  12. # output how much strings are new/changed.
  13. #
  14. # * lang_stats: output statistics for each catalog/language combination
  15. #
  16. # * fetch: fetch translations from transifex.com
  17. #
  18. # * commit: update resources in transifex.com with the local files
  19. #
  20. # Each command support the --languages and --resources options to limit their
  21. # operation to the specified language or resource. For example, to get stats
  22. # for Spanish in contrib.admin, run:
  23. #
  24. # $ python scripts/manage_translations.py lang_stats --language=es --resources=taiga
  25. import os
  26. from argparse import ArgumentParser
  27. from argparse import RawTextHelpFormatter
  28. from subprocess import PIPE, Popen, call
  29. from django_jinja.management.commands import makemessages
  30. def _get_locale_dirs(resources):
  31. """
  32. Return a tuple (app name, absolute path) for all locale directories.
  33. If resources list is not None, filter directories matching resources content.
  34. """
  35. contrib_dir = os.getcwd()
  36. dirs = []
  37. # Collect all locale directories
  38. for contrib_name in os.listdir(contrib_dir):
  39. path = os.path.join(contrib_dir, contrib_name, "locale")
  40. if os.path.isdir(path):
  41. dirs.append((contrib_name, path))
  42. # Filter by resources, if any
  43. if resources is not None:
  44. res_names = [d[0] for d in dirs]
  45. dirs = [ld for ld in dirs if ld[0] in resources]
  46. if len(resources) > len(dirs):
  47. print("You have specified some unknown resources. "
  48. "Available resource names are: {0}".format(", ".join(res_names)))
  49. exit(1)
  50. return dirs
  51. def _tx_resource_for_name(name):
  52. """ Return the Transifex resource name """
  53. return "taiga-back.{}".format(name)
  54. def _check_diff(cat_name, base_path):
  55. """
  56. Output the approximate number of changed/added strings in the en catalog.
  57. """
  58. po_path = "{path}/en/LC_MESSAGES/django.po".format(path=base_path)
  59. p = Popen("git diff -U0 {0} | egrep '^[-+]msgid' | wc -l".format(po_path),
  60. stdout=PIPE, stderr=PIPE, shell=True)
  61. output, errors = p.communicate()
  62. num_changes = int(output.strip())
  63. print("{0} changed/added messages in '{1}' catalog.".format(num_changes, cat_name))
  64. def update_catalogs(resources=None, languages=None):
  65. """
  66. Update the en/LC_MESSAGES/django.po (all) files with
  67. new/updated translatable strings.
  68. """
  69. cmd = makemessages.Command()
  70. opts = {
  71. "locale": ["en"],
  72. "extensions": ["py", "jinja"],
  73. # Default values
  74. "domain": "django",
  75. "all": False,
  76. "symlinks": False,
  77. "ignore_patterns": [],
  78. "use_default_ignore_patterns": True,
  79. "no_wrap": False,
  80. "no_location": False,
  81. "no_obsolete": False,
  82. "keep_pot": False,
  83. "verbosity": "0",
  84. }
  85. if resources is not None:
  86. print("`update_catalogs` will always process all resources.")
  87. os.chdir(os.getcwd())
  88. print("Updating en catalogs for all taiga-back resourcess...")
  89. cmd.handle(**opts)
  90. # Output changed stats
  91. contrib_dirs = _get_locale_dirs(None)
  92. for name, dir_ in contrib_dirs:
  93. _check_diff(name, dir_)
  94. def lang_stats(resources=None, languages=None):
  95. """
  96. Output language statistics of committed translation files for each catalog.
  97. If resources is provided, it should be a list of translation resource to
  98. limit the output (e.g. ['main', 'taiga']).
  99. """
  100. locale_dirs = _get_locale_dirs(resources)
  101. for name, dir_ in locale_dirs:
  102. print("\nShowing translations stats for '{res}':".format(res=name))
  103. langs = []
  104. for d in os.listdir(dir_):
  105. if not d.startswith('_') and os.path.isdir(os.path.join(dir_, d)):
  106. langs.append(d)
  107. langs = sorted(langs)
  108. for lang in langs:
  109. if languages and lang not in languages:
  110. continue
  111. # TODO: merge first with the latest en catalog
  112. p = Popen("msgfmt -vc -o /dev/null {path}/{lang}/LC_MESSAGES/django.po".format(path=dir_, lang=lang),
  113. stdout=PIPE, stderr=PIPE, shell=True)
  114. output, errors = p.communicate()
  115. if p.returncode == 0:
  116. # msgfmt output stats on stderr
  117. print("{0}: {1}".format(lang, errors.strip().decode("utf-8")))
  118. else:
  119. print("Errors happened when checking {0} translation for {1}:\n{2}".format(lang, name, errors))
  120. def fetch(resources=None, languages=None):
  121. """
  122. Fetch translations from Transifex, wrap long lines, generate mo files.
  123. """
  124. locale_dirs = _get_locale_dirs(resources)
  125. errors = []
  126. for name, dir_ in locale_dirs:
  127. # Transifex pull
  128. if languages is None:
  129. call("tx pull -r {res} -f --minimum-perc=5".format(res=_tx_resource_for_name(name)), shell=True)
  130. languages = sorted([d for d in os.listdir(dir_) if not d.startswith("_") and os.path.isdir(os.path.join(dir_, d)) and d != "en"])
  131. else:
  132. for lang in languages:
  133. call("tx pull -r {res} -f -l {lang}".format(res=_tx_resource_for_name(name), lang=lang), shell=True)
  134. # msgcat to wrap lines and msgfmt for compilation of .mo file
  135. for lang in languages:
  136. po_path = "{path}/{lang}/LC_MESSAGES/django.po".format(path=dir_, lang=lang)
  137. if not os.path.exists(po_path):
  138. print("No {lang} translation for resource {res}".format(lang=lang, res=name))
  139. continue
  140. call("msgcat -o {0} {0}".format(po_path), shell=True)
  141. res = call("msgfmt -c -o {0}.mo {1}".format(po_path[:-3], po_path), shell=True)
  142. if res != 0:
  143. errors.append((name, lang))
  144. if errors:
  145. print("\nWARNING: Errors have occurred in following cases:")
  146. for resource, lang in errors:
  147. print("\tResource {res} for language {lang}".format(res=resource, lang=lang))
  148. exit(1)
  149. def regenerate(resources=None, languages=None):
  150. """
  151. Wrap long lines and generate mo files.
  152. """
  153. locale_dirs = _get_locale_dirs(resources)
  154. errors = []
  155. for name, dir_ in locale_dirs:
  156. if languages is None:
  157. languages = sorted([d for d in os.listdir(dir_) if not d.startswith("_") and os.path.isdir(os.path.join(dir_, d)) and d != "en"])
  158. for lang in languages:
  159. po_path = "{path}/{lang}/LC_MESSAGES/django.po".format(path=dir_, lang=lang)
  160. if not os.path.exists(po_path):
  161. print("No {lang} translation for resource {res}".format(lang=lang, res=name))
  162. continue
  163. call("msgcat -o {0} {0}".format(po_path), shell=True)
  164. res = call("msgfmt -c -o {0}.mo {1}".format(po_path[:-3], po_path), shell=True)
  165. if res != 0:
  166. errors.append((name, lang))
  167. if errors:
  168. print("\nWARNING: Errors have occurred in following cases:")
  169. for resource, lang in errors:
  170. print("\tResource {res} for language {lang}".format(res=resource, lang=lang))
  171. exit(1)
  172. def commit(resources=None, languages=None):
  173. """
  174. Commit messages to Transifex,
  175. """
  176. locale_dirs = _get_locale_dirs(resources)
  177. errors = []
  178. for name, dir_ in locale_dirs:
  179. # Transifex push
  180. if languages is None:
  181. call("tx push -r {res} -s -l en".format(res=_tx_resource_for_name(name)), shell=True)
  182. else:
  183. for lang in languages:
  184. call("tx push -r {res} -l {lang}".format(res= _tx_resource_for_name(name), lang=lang), shell=True)
  185. if __name__ == "__main__":
  186. try:
  187. devnull = open(os.devnull)
  188. Popen(["tx"], stdout=devnull, stderr=devnull).communicate()
  189. except OSError as e:
  190. if e.errno == os.errno.ENOENT:
  191. print("""
  192. You need transifex-client, install it.
  193. 1. Install transifex-client, use
  194. $ pip install --upgrade -r requirements-devel.txt
  195. or
  196. $ pip install --upgrade transifex-client==0.11.1.beta
  197. 2. Create ~/.transifexrc file:
  198. $ vim ~/.transifexrc"
  199. [https://www.transifex.com]
  200. hostname = https://www.transifex.com
  201. token =
  202. username = <YOUR_USERNAME>
  203. password = <YOUR_PASSWOR>
  204. """)
  205. exit(1)
  206. RUNABLE_SCRIPTS = {
  207. "update_catalogs": "regenerate .po files of main lang (en).",
  208. "commit": "send .po file to transifex ('en' by default).",
  209. "fetch": "get .po files from transifex and regenerate .mo files.",
  210. "regenerate": "regenerate .mo files.",
  211. "lang_stats": "get stats of local translations",
  212. }
  213. parser = ArgumentParser(description="manage translations in taiga-back between the repo and transifex.",
  214. formatter_class=RawTextHelpFormatter)
  215. parser.add_argument("cmd", nargs=1,
  216. help="\n".join(["{0} - {1}".format(c, h) for c, h in RUNABLE_SCRIPTS.items()]))
  217. parser.add_argument("-r", "--resources", action="append",
  218. help="limit operation to the specified resources")
  219. parser.add_argument("-l", "--languages", action="append",
  220. help="limit operation to the specified languages")
  221. options = parser.parse_args()
  222. if options.cmd[0] in RUNABLE_SCRIPTS.keys():
  223. eval(options.cmd[0])(options.resources, options.languages)
  224. else:
  225. print("Available commands are: {}".format(", ".join(RUNABLE_SCRIPTS.keys())))