|
0
|
1 |
import os |
|
|
2 |
import sys |
|
|
3 |
from optparse import make_option |
|
|
4 |
from django.core.management.base import BaseCommand, CommandError |
|
|
5 |
|
|
|
6 |
def compile_messages(locale=None): |
|
|
7 |
basedirs = [os.path.join('conf', 'locale'), 'locale'] |
|
|
8 |
if os.environ.get('DJANGO_SETTINGS_MODULE'): |
|
|
9 |
from django.conf import settings |
|
|
10 |
basedirs.extend(settings.LOCALE_PATHS) |
|
|
11 |
|
|
|
12 |
# Gather existing directories. |
|
|
13 |
basedirs = set(map(os.path.abspath, filter(os.path.isdir, basedirs))) |
|
|
14 |
|
|
|
15 |
if not basedirs: |
|
|
16 |
raise CommandError("This script should be run from the Django SVN tree or your project or app tree, or with the settings module specified.") |
|
|
17 |
|
|
|
18 |
for basedir in basedirs: |
|
|
19 |
if locale: |
|
|
20 |
basedir = os.path.join(basedir, locale, 'LC_MESSAGES') |
|
|
21 |
for dirpath, dirnames, filenames in os.walk(basedir): |
|
|
22 |
for f in filenames: |
|
|
23 |
if f.endswith('.po'): |
|
|
24 |
sys.stderr.write('processing file %s in %s\n' % (f, dirpath)) |
|
|
25 |
pf = os.path.splitext(os.path.join(dirpath, f))[0] |
|
|
26 |
# Store the names of the .mo and .po files in an environment |
|
|
27 |
# variable, rather than doing a string replacement into the |
|
|
28 |
# command, so that we can take advantage of shell quoting, to |
|
|
29 |
# quote any malicious characters/escaping. |
|
|
30 |
# See http://cyberelk.net/tim/articles/cmdline/ar01s02.html |
|
|
31 |
os.environ['djangocompilemo'] = pf + '.mo' |
|
|
32 |
os.environ['djangocompilepo'] = pf + '.po' |
|
|
33 |
if sys.platform == 'win32': # Different shell-variable syntax |
|
|
34 |
cmd = 'msgfmt --check-format -o "%djangocompilemo%" "%djangocompilepo%"' |
|
|
35 |
else: |
|
|
36 |
cmd = 'msgfmt --check-format -o "$djangocompilemo" "$djangocompilepo"' |
|
|
37 |
os.system(cmd) |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
class Command(BaseCommand): |
|
|
41 |
option_list = BaseCommand.option_list + ( |
|
|
42 |
make_option('--locale', '-l', dest='locale', |
|
|
43 |
help='The locale to process. Default is to process all.'), |
|
|
44 |
) |
|
|
45 |
help = 'Compiles .po files to .mo files for use with builtin gettext support.' |
|
|
46 |
|
|
|
47 |
requires_model_validation = False |
|
|
48 |
can_import_settings = False |
|
|
49 |
|
|
|
50 |
def handle(self, **options): |
|
|
51 |
locale = options.get('locale') |
|
|
52 |
compile_messages(locale) |