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