manage_translations.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  2. #
  3. # This python file contains utility scripts to manage Django translations.
  4. # It has to be run inside the django git root directory.
  5. #
  6. # The following commands are available:
  7. #
  8. # * update_catalogs: check for new strings in core and contrib catalogs, and
  9. # output how much strings are new/changed.
  10. #
  11. # * lang_stats: output statistics for each catalog/language combination
  12. #
  13. # * fetch: fetch translations from transifex.com
  14. #
  15. # Each command support the --languages and --resources options to limit their
  16. # operation to the specified language or resource. For example, to get stats
  17. # for Spanish in contrib.admin, run:
  18. #
  19. # $ python scripts/manage_translations.py lang_stats --language=es --resources=admin
  20. import os
  21. from optparse import OptionParser
  22. from subprocess import call, Popen, PIPE
  23. from django.core.management import call_command
  24. HAVE_JS = ['admin']
  25. def _get_locale_dirs(include_core=True):
  26. """
  27. Return a tuple (contrib name, absolute path) for all locale directories,
  28. optionally including the django core catalog.
  29. """
  30. contrib_dir = os.path.join(os.getcwd(), 'django', 'contrib')
  31. dirs = []
  32. for contrib_name in os.listdir(contrib_dir):
  33. path = os.path.join(contrib_dir, contrib_name, 'locale')
  34. if os.path.isdir(path):
  35. dirs.append((contrib_name, path))
  36. if contrib_name in HAVE_JS:
  37. dirs.append(("%s-js" % contrib_name, path))
  38. if include_core:
  39. dirs.insert(0, ('core', os.path.join(os.getcwd(), 'django', 'conf', 'locale')))
  40. return dirs
  41. def _tx_resource_for_name(name):
  42. """ Return the Transifex resource name """
  43. if name == 'core':
  44. return "django-core.core"
  45. else:
  46. return "django-core.contrib-%s" % name
  47. def _check_diff(cat_name, base_path):
  48. """
  49. Output the approximate number of changed/added strings in the en catalog.
  50. """
  51. po_path = '%(path)s/en/LC_MESSAGES/django%(ext)s.po' % {
  52. 'path': base_path, 'ext': 'js' if cat_name.endswith('-js') else ''}
  53. p = Popen("git diff -U0 %s | egrep -v '^@@|^[-+]#|^..POT-Creation' | wc -l" % po_path,
  54. stdout=PIPE, stderr=PIPE, shell=True)
  55. output, errors = p.communicate()
  56. num_changes = int(output.strip()) - 4
  57. print("%d changed/added messages in '%s' catalog." % (num_changes, cat_name))
  58. def update_catalogs(resources=None, languages=None):
  59. """
  60. Update the en/LC_MESSAGES/django.po (main and contrib) files with
  61. new/updated translatable strings.
  62. """
  63. contrib_dirs = _get_locale_dirs(include_core=False)
  64. os.chdir(os.path.join(os.getcwd(), 'django'))
  65. print("Updating main en catalog")
  66. call_command('makemessages', locale='en')
  67. _check_diff('core', os.path.join(os.getcwd(), 'conf', 'locale'))
  68. # Contrib catalogs
  69. for name, dir_ in contrib_dirs:
  70. if resources and not name in resources:
  71. continue
  72. os.chdir(os.path.join(dir_, '..'))
  73. print("Updating en catalog in %s" % dir_)
  74. if name.endswith('-js'):
  75. call_command('makemessages', locale='en', domain='djangojs')
  76. else:
  77. call_command('makemessages', locale='en')
  78. _check_diff(name, dir_)
  79. def lang_stats(resources=None, languages=None):
  80. """
  81. Output language statistics of committed translation files for each
  82. Django catalog.
  83. If resources is provided, it should be a list of translation resource to
  84. limit the output (e.g. ['core', 'gis']).
  85. """
  86. locale_dirs = _get_locale_dirs()
  87. for name, dir_ in locale_dirs:
  88. if resources and not name in resources:
  89. continue
  90. print("\nShowing translations stats for '%s':" % name)
  91. langs = sorted([d for d in os.listdir(dir_) if not d.startswith('_')])
  92. for lang in langs:
  93. if languages and not lang in languages:
  94. continue
  95. # TODO: merge first with the latest en catalog
  96. p = Popen("msgfmt -vc -o /dev/null %(path)s/%(lang)s/LC_MESSAGES/django%(ext)s.po" % {
  97. 'path': dir_, 'lang': lang, 'ext': 'js' if name.endswith('-js') else ''},
  98. stdout=PIPE, stderr=PIPE, shell=True)
  99. output, errors = p.communicate()
  100. if p.returncode == 0:
  101. # msgfmt output stats on stderr
  102. print("%s: %s" % (lang, errors.strip()))
  103. else:
  104. print("Errors happened when checking %s translation for %s:\n%s" % (
  105. lang, name, errors))
  106. def fetch(resources=None, languages=None):
  107. """
  108. Fetch translations from Transifex, wrap long lines, generate mo files.
  109. """
  110. locale_dirs = _get_locale_dirs()
  111. errors = []
  112. for name, dir_ in locale_dirs:
  113. if resources and not name in resources:
  114. continue
  115. # Transifex pull
  116. if languages is None:
  117. call('tx pull -r %(res)s -a -f' % {'res': _tx_resource_for_name(name)}, shell=True)
  118. languages = sorted([d for d in os.listdir(dir_) if not d.startswith('_')])
  119. else:
  120. for lang in languages:
  121. call('tx pull -r %(res)s -f -l %(lang)s' % {
  122. 'res': _tx_resource_for_name(name), 'lang': lang}, shell=True)
  123. # msgcat to wrap lines and msgfmt for compilation of .mo file
  124. for lang in languages:
  125. po_path = '%(path)s/%(lang)s/LC_MESSAGES/django%(ext)s.po' % {
  126. 'path': dir_, 'lang': lang, 'ext': 'js' if name.endswith('-js') else ''}
  127. call('msgcat -o %s %s' % (po_path, po_path), shell=True)
  128. res = call('msgfmt -c -o %s.mo %s' % (po_path[:-3], po_path), shell=True)
  129. if res != 0:
  130. errors.append((name, lang))
  131. if errors:
  132. print("\nWARNING: Errors have occurred in following cases:")
  133. for resource, lang in errors:
  134. print("\tResource %s for language %s" % (resource, lang))
  135. exit(1)
  136. if __name__ == "__main__":
  137. RUNABLE_SCRIPTS = ('update_catalogs', 'lang_stats', 'fetch')
  138. parser = OptionParser(usage="usage: %prog [options] cmd")
  139. parser.add_option("-r", "--resources", action='append',
  140. help="limit operation to the specified resources")
  141. parser.add_option("-l", "--languages", action='append',
  142. help="limit operation to the specified languages")
  143. options, args = parser.parse_args()
  144. if not args:
  145. parser.print_usage()
  146. exit(1)
  147. if args[0] in RUNABLE_SCRIPTS:
  148. eval(args[0])(options.resources, options.languages)
  149. else:
  150. print("Available commands are: %s" % ", ".join(RUNABLE_SCRIPTS))