2017-03-07 16:10:32 +01:00
|
|
|
#!/usr/bin/env python
|
2013-01-01 16:17:26 +01:00
|
|
|
#
|
|
|
|
# This python file contains utility scripts to manage Django translations.
|
|
|
|
# It has to be run inside the django git root directory.
|
|
|
|
#
|
|
|
|
# The following commands are available:
|
|
|
|
#
|
|
|
|
# * update_catalogs: check for new strings in core and contrib catalogs, and
|
|
|
|
# output how much strings are new/changed.
|
|
|
|
#
|
|
|
|
# * lang_stats: output statistics for each catalog/language combination
|
|
|
|
#
|
|
|
|
# * fetch: fetch translations from transifex.com
|
|
|
|
#
|
|
|
|
# Each command support the --languages and --resources options to limit their
|
|
|
|
# operation to the specified language or resource. For example, to get stats
|
|
|
|
# for Spanish in contrib.admin, run:
|
|
|
|
#
|
|
|
|
# $ python scripts/manage_translations.py lang_stats --language=es --resources=admin
|
|
|
|
|
|
|
|
import os
|
2015-01-28 13:35:27 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from subprocess import PIPE, Popen, call
|
2013-01-01 16:17:26 +01:00
|
|
|
|
2016-08-25 23:11:51 +02:00
|
|
|
import django
|
|
|
|
from django.conf import settings
|
2013-01-01 16:17:26 +01:00
|
|
|
from django.core.management import call_command
|
|
|
|
|
|
|
|
HAVE_JS = ['admin']
|
|
|
|
|
2013-11-03 01:37:15 +01:00
|
|
|
|
2013-12-12 15:16:21 +01:00
|
|
|
def _get_locale_dirs(resources, include_core=True):
|
2013-01-01 16:17:26 +01:00
|
|
|
"""
|
|
|
|
Return a tuple (contrib name, absolute path) for all locale directories,
|
|
|
|
optionally including the django core catalog.
|
2013-12-12 15:16:21 +01:00
|
|
|
If resources list is not None, filter directories matching resources content.
|
2013-01-01 16:17:26 +01:00
|
|
|
"""
|
|
|
|
contrib_dir = os.path.join(os.getcwd(), 'django', 'contrib')
|
|
|
|
dirs = []
|
2013-12-12 15:16:21 +01:00
|
|
|
|
|
|
|
# Collect all locale directories
|
2013-01-01 16:17:26 +01:00
|
|
|
for contrib_name in os.listdir(contrib_dir):
|
|
|
|
path = os.path.join(contrib_dir, contrib_name, 'locale')
|
|
|
|
if os.path.isdir(path):
|
|
|
|
dirs.append((contrib_name, path))
|
|
|
|
if contrib_name in HAVE_JS:
|
|
|
|
dirs.append(("%s-js" % contrib_name, path))
|
|
|
|
if include_core:
|
|
|
|
dirs.insert(0, ('core', os.path.join(os.getcwd(), 'django', 'conf', 'locale')))
|
2013-12-12 15:16:21 +01:00
|
|
|
|
|
|
|
# Filter by resources, if any
|
|
|
|
if resources is not None:
|
|
|
|
res_names = [d[0] for d in dirs]
|
|
|
|
dirs = [ld for ld in dirs if ld[0] in resources]
|
|
|
|
if len(resources) > len(dirs):
|
|
|
|
print("You have specified some unknown resources. "
|
|
|
|
"Available resource names are: %s" % (', '.join(res_names),))
|
|
|
|
exit(1)
|
2013-01-01 16:17:26 +01:00
|
|
|
return dirs
|
|
|
|
|
2013-11-03 01:37:15 +01:00
|
|
|
|
2013-01-01 16:17:26 +01:00
|
|
|
def _tx_resource_for_name(name):
|
|
|
|
""" Return the Transifex resource name """
|
|
|
|
if name == 'core':
|
2014-04-24 11:36:25 +02:00
|
|
|
return "django.core"
|
2013-01-01 16:17:26 +01:00
|
|
|
else:
|
2014-04-24 11:36:25 +02:00
|
|
|
return "django.contrib-%s" % name
|
2013-01-01 16:17:26 +01:00
|
|
|
|
2013-11-03 01:37:15 +01:00
|
|
|
|
2013-01-01 16:17:26 +01:00
|
|
|
def _check_diff(cat_name, base_path):
|
|
|
|
"""
|
|
|
|
Output the approximate number of changed/added strings in the en catalog.
|
|
|
|
"""
|
|
|
|
po_path = '%(path)s/en/LC_MESSAGES/django%(ext)s.po' % {
|
|
|
|
'path': base_path, 'ext': 'js' if cat_name.endswith('-js') else ''}
|
2014-05-19 15:11:11 +02:00
|
|
|
p = Popen("git diff -U0 %s | egrep '^[-+]msgid' | wc -l" % po_path,
|
2013-01-01 16:17:26 +01:00
|
|
|
stdout=PIPE, stderr=PIPE, shell=True)
|
|
|
|
output, errors = p.communicate()
|
2014-05-19 15:11:11 +02:00
|
|
|
num_changes = int(output.strip())
|
2013-01-01 16:17:26 +01:00
|
|
|
print("%d changed/added messages in '%s' catalog." % (num_changes, cat_name))
|
|
|
|
|
|
|
|
|
|
|
|
def update_catalogs(resources=None, languages=None):
|
|
|
|
"""
|
|
|
|
Update the en/LC_MESSAGES/django.po (main and contrib) files with
|
|
|
|
new/updated translatable strings.
|
|
|
|
"""
|
2016-08-25 23:11:51 +02:00
|
|
|
settings.configure()
|
|
|
|
django.setup()
|
2014-05-19 09:26:31 +02:00
|
|
|
if resources is not None:
|
|
|
|
print("`update_catalogs` will always process all resources.")
|
|
|
|
contrib_dirs = _get_locale_dirs(None, include_core=False)
|
2013-01-01 16:17:26 +01:00
|
|
|
|
|
|
|
os.chdir(os.path.join(os.getcwd(), 'django'))
|
2014-05-19 09:26:31 +02:00
|
|
|
print("Updating en catalogs for Django and contrib apps...")
|
2014-01-04 23:06:50 +01:00
|
|
|
call_command('makemessages', locale=['en'])
|
2014-05-19 15:11:11 +02:00
|
|
|
print("Updating en JS catalogs for Django and contrib apps...")
|
|
|
|
call_command('makemessages', locale=['en'], domain='djangojs')
|
2013-01-01 16:17:26 +01:00
|
|
|
|
2014-05-19 15:11:11 +02:00
|
|
|
# Output changed stats
|
|
|
|
_check_diff('core', os.path.join(os.getcwd(), 'conf', 'locale'))
|
2013-01-01 16:17:26 +01:00
|
|
|
for name, dir_ in contrib_dirs:
|
|
|
|
_check_diff(name, dir_)
|
|
|
|
|
|
|
|
|
|
|
|
def lang_stats(resources=None, languages=None):
|
|
|
|
"""
|
|
|
|
Output language statistics of committed translation files for each
|
|
|
|
Django catalog.
|
|
|
|
If resources is provided, it should be a list of translation resource to
|
|
|
|
limit the output (e.g. ['core', 'gis']).
|
|
|
|
"""
|
2013-12-12 15:16:21 +01:00
|
|
|
locale_dirs = _get_locale_dirs(resources)
|
2013-01-01 16:17:26 +01:00
|
|
|
|
|
|
|
for name, dir_ in locale_dirs:
|
2013-10-10 22:42:30 +02:00
|
|
|
print("\nShowing translations stats for '%s':" % name)
|
2017-06-02 01:08:59 +02:00
|
|
|
langs = sorted(d for d in os.listdir(dir_) if not d.startswith('_'))
|
2013-01-01 16:17:26 +01:00
|
|
|
for lang in langs:
|
2014-03-30 21:11:05 +02:00
|
|
|
if languages and lang not in languages:
|
2013-01-01 16:17:26 +01:00
|
|
|
continue
|
|
|
|
# TODO: merge first with the latest en catalog
|
|
|
|
p = Popen("msgfmt -vc -o /dev/null %(path)s/%(lang)s/LC_MESSAGES/django%(ext)s.po" % {
|
|
|
|
'path': dir_, 'lang': lang, 'ext': 'js' if name.endswith('-js') else ''},
|
|
|
|
stdout=PIPE, stderr=PIPE, shell=True)
|
|
|
|
output, errors = p.communicate()
|
|
|
|
if p.returncode == 0:
|
|
|
|
# msgfmt output stats on stderr
|
|
|
|
print("%s: %s" % (lang, errors.strip()))
|
2013-07-03 16:03:11 +02:00
|
|
|
else:
|
|
|
|
print("Errors happened when checking %s translation for %s:\n%s" % (
|
|
|
|
lang, name, errors))
|
2013-01-01 16:17:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
def fetch(resources=None, languages=None):
|
|
|
|
"""
|
|
|
|
Fetch translations from Transifex, wrap long lines, generate mo files.
|
|
|
|
"""
|
2013-12-12 15:16:21 +01:00
|
|
|
locale_dirs = _get_locale_dirs(resources)
|
2013-07-03 16:03:11 +02:00
|
|
|
errors = []
|
2013-01-01 16:17:26 +01:00
|
|
|
|
|
|
|
for name, dir_ in locale_dirs:
|
|
|
|
# Transifex pull
|
|
|
|
if languages is None:
|
2013-12-12 15:16:21 +01:00
|
|
|
call('tx pull -r %(res)s -a -f --minimum-perc=5' % {'res': _tx_resource_for_name(name)}, shell=True)
|
2017-06-02 01:08:59 +02:00
|
|
|
target_langs = sorted(d for d in os.listdir(dir_) if not d.startswith('_') and d != 'en')
|
2013-01-01 16:17:26 +01:00
|
|
|
else:
|
|
|
|
for lang in languages:
|
|
|
|
call('tx pull -r %(res)s -f -l %(lang)s' % {
|
|
|
|
'res': _tx_resource_for_name(name), 'lang': lang}, shell=True)
|
2015-04-01 14:42:04 +02:00
|
|
|
target_langs = languages
|
2013-01-01 16:17:26 +01:00
|
|
|
|
|
|
|
# msgcat to wrap lines and msgfmt for compilation of .mo file
|
2015-04-01 14:42:04 +02:00
|
|
|
for lang in target_langs:
|
2013-01-01 16:17:26 +01:00
|
|
|
po_path = '%(path)s/%(lang)s/LC_MESSAGES/django%(ext)s.po' % {
|
|
|
|
'path': dir_, 'lang': lang, 'ext': 'js' if name.endswith('-js') else ''}
|
2013-12-12 15:16:21 +01:00
|
|
|
if not os.path.exists(po_path):
|
|
|
|
print("No %(lang)s translation for resource %(name)s" % {
|
|
|
|
'lang': lang, 'name': name})
|
|
|
|
continue
|
2015-04-01 14:42:04 +02:00
|
|
|
call('msgcat --no-location -o %s %s' % (po_path, po_path), shell=True)
|
2013-07-03 16:03:11 +02:00
|
|
|
res = call('msgfmt -c -o %s.mo %s' % (po_path[:-3], po_path), shell=True)
|
|
|
|
if res != 0:
|
|
|
|
errors.append((name, lang))
|
|
|
|
if errors:
|
|
|
|
print("\nWARNING: Errors have occurred in following cases:")
|
|
|
|
for resource, lang in errors:
|
|
|
|
print("\tResource %s for language %s" % (resource, lang))
|
|
|
|
exit(1)
|
2013-01-01 16:17:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
RUNABLE_SCRIPTS = ('update_catalogs', 'lang_stats', 'fetch')
|
|
|
|
|
2014-06-06 21:12:18 +02:00
|
|
|
parser = ArgumentParser()
|
2015-10-09 17:40:06 +02:00
|
|
|
parser.add_argument('cmd', nargs=1, choices=RUNABLE_SCRIPTS)
|
2016-03-29 00:33:29 +02:00
|
|
|
parser.add_argument("-r", "--resources", action='append', help="limit operation to the specified resources")
|
|
|
|
parser.add_argument("-l", "--languages", action='append', help="limit operation to the specified languages")
|
2014-06-06 21:12:18 +02:00
|
|
|
options = parser.parse_args()
|
2013-01-01 16:17:26 +01:00
|
|
|
|
2015-10-09 17:40:06 +02:00
|
|
|
eval(options.cmd[0])(options.resources, options.languages)
|