2012-08-09 05:40:20 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-10-11 00:38:28 +08:00
|
|
|
import codecs
|
2008-07-06 14:39:44 +08:00
|
|
|
import os
|
|
|
|
from optparse import make_option
|
2013-02-13 00:58:49 +08:00
|
|
|
|
2008-07-26 11:01:33 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2013-02-13 03:50:47 +08:00
|
|
|
from django.core.management.utils import find_command, popen_wrapper
|
2013-11-11 18:49:48 +08:00
|
|
|
from django.utils._os import npath, upath
|
2008-07-06 14:39:44 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-10-11 00:38:28 +08:00
|
|
|
def has_bom(fn):
|
2012-05-26 02:37:38 +08:00
|
|
|
with open(fn, 'rb') as f:
|
2012-05-05 20:01:38 +08:00
|
|
|
sample = f.read(4)
|
2012-08-09 05:40:20 +08:00
|
|
|
return sample[:3] == b'\xef\xbb\xbf' or \
|
2013-12-13 04:23:24 +08:00
|
|
|
sample.startswith(codecs.BOM_UTF16_LE) or \
|
|
|
|
sample.startswith(codecs.BOM_UTF16_BE)
|
2010-10-11 00:38:28 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-03-17 02:48:40 +08:00
|
|
|
def compile_messages(stdout, locale=None):
|
2013-02-13 03:50:47 +08:00
|
|
|
program = 'msgfmt'
|
|
|
|
if find_command(program) is None:
|
|
|
|
raise CommandError("Can't find %s. Make sure you have GNU gettext tools 0.15 or newer installed." % program)
|
|
|
|
|
2008-08-25 11:26:37 +08:00
|
|
|
basedirs = [os.path.join('conf', 'locale'), 'locale']
|
2008-07-06 14:39:44 +08:00
|
|
|
if os.environ.get('DJANGO_SETTINGS_MODULE'):
|
|
|
|
from django.conf import settings
|
2013-11-11 18:49:48 +08:00
|
|
|
basedirs.extend([upath(path) for path in settings.LOCALE_PATHS])
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
# Gather existing directories.
|
|
|
|
basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs)))
|
|
|
|
|
|
|
|
if not basedirs:
|
2012-04-28 11:37:59 +08:00
|
|
|
raise CommandError("This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.")
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
for basedir in basedirs:
|
|
|
|
if locale:
|
2013-11-23 10:47:04 +08:00
|
|
|
dirs = [os.path.join(basedir, l, 'LC_MESSAGES') for l in locale]
|
2012-06-07 17:23:25 +08:00
|
|
|
else:
|
|
|
|
dirs = [basedir]
|
|
|
|
for ldir in dirs:
|
|
|
|
for dirpath, dirnames, filenames in os.walk(ldir):
|
|
|
|
for f in filenames:
|
|
|
|
if not f.endswith('.po'):
|
|
|
|
continue
|
2013-03-17 02:48:40 +08:00
|
|
|
stdout.write('processing file %s in %s\n' % (f, dirpath))
|
2010-10-11 00:38:28 +08:00
|
|
|
fn = os.path.join(dirpath, f)
|
|
|
|
if has_bom(fn):
|
|
|
|
raise CommandError("The %s file has a BOM (Byte Order Mark). Django only supports .po files encoded in UTF-8 and without any BOM." % fn)
|
|
|
|
pf = os.path.splitext(fn)[0]
|
2013-02-13 00:58:49 +08:00
|
|
|
args = [program, '--check-format', '-o', npath(pf + '.mo'), npath(pf + '.po')]
|
|
|
|
output, errors, status = popen_wrapper(args)
|
|
|
|
if status:
|
|
|
|
if errors:
|
|
|
|
msg = "Execution of %s failed: %s" % (program, errors)
|
|
|
|
else:
|
|
|
|
msg = "Execution of %s failed" % program
|
|
|
|
raise CommandError(msg)
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
option_list = BaseCommand.option_list + (
|
2012-06-07 17:23:25 +08:00
|
|
|
make_option('--locale', '-l', dest='locale', action='append',
|
2013-11-23 10:47:04 +08:00
|
|
|
help='locale(s) to process (e.g. de_AT). Default is to process all. Can be used multiple times.'),
|
2008-07-06 14:39:44 +08:00
|
|
|
)
|
|
|
|
help = 'Compiles .po files to .mo files for use with builtin gettext support.'
|
|
|
|
|
|
|
|
requires_model_validation = False
|
2013-02-04 07:53:48 +08:00
|
|
|
leave_locale_alone = True
|
2008-07-06 14:39:44 +08:00
|
|
|
|
|
|
|
def handle(self, **options):
|
|
|
|
locale = options.get('locale')
|
2013-03-17 02:48:40 +08:00
|
|
|
compile_messages(self.stdout, locale=locale)
|