|
| 1 | +#-*- coding: utf-8 -*- |
| 2 | +import os |
| 3 | +from distutils.dist import Distribution |
| 4 | +from optparse import make_option |
| 5 | +from subprocess import call |
| 6 | + |
| 7 | +from django.core.management.base import LabelCommand, CommandError |
| 8 | +from django.conf import settings |
| 9 | + |
| 10 | + |
| 11 | +class Command(LabelCommand): |
| 12 | + |
| 13 | + args = '[makemessages] [compilemessages]' |
| 14 | + |
| 15 | + option_list = LabelCommand.option_list + ( |
| 16 | + make_option('--locale', '-l', default=None, dest='locale', action='append', |
| 17 | + help='Creates or updates the message files for the given locale(s) (e.g. pt_BR). ' |
| 18 | + 'Can be used multiple times.'), |
| 19 | + make_option('--domain', '-d', default='django', dest='domain', |
| 20 | + help='The domain of the message files (default: "django").'), |
| 21 | + make_option('--mapping-file', '-F', default=None, dest='mapping_file', |
| 22 | + help='Mapping file') |
| 23 | + ) |
| 24 | + |
| 25 | + def handle_label(self, command, **options): |
| 26 | + if command not in ('makemessages', 'compilemessages'): |
| 27 | + raise CommandError("You must either apply 'makemessages' or 'compilemessages'") |
| 28 | + |
| 29 | + if command == 'makemessages': |
| 30 | + self.handle_makemessages(**options) |
| 31 | + |
| 32 | + def handle_makemessages(self, **options): |
| 33 | + locale_paths = list(settings.LOCALE_PATHS) |
| 34 | + domain = options.pop('domain') |
| 35 | + locales = options.pop('locale') |
| 36 | + |
| 37 | + # support for mapping file specification via setup.cfg |
| 38 | + # TODO: Try to support all possible options. |
| 39 | + distribution = Distribution() |
| 40 | + distribution.parse_config_files(distribution.find_config_files()) |
| 41 | + |
| 42 | + mapping_file = options.pop('mapping_file', None) |
| 43 | + if mapping_file is None and 'extract_messages' in distribution.command_options: |
| 44 | + opts = distribution.command_options['extract_messages'] |
| 45 | + try: |
| 46 | + mapping_file = opts.get('mapping_file', ())[1] |
| 47 | + except IndexError: |
| 48 | + mapping_file = None |
| 49 | + |
| 50 | + for path in locale_paths: |
| 51 | + potfile = os.path.join(path, '%s.pot' % domain) |
| 52 | + if not os.path.exists(potfile): |
| 53 | + continue |
| 54 | + |
| 55 | + cmd = ['pybabel', 'extract', '-o', |
| 56 | + os.path.join(path, '%s.pot' % domain)] |
| 57 | + |
| 58 | + if mapping_file is not None: |
| 59 | + cmd.extend(['-F', mapping_file]) |
| 60 | + |
| 61 | + cmd.append(os.path.dirname(path)) |
| 62 | + |
| 63 | + print cmd |
| 64 | + call(cmd) |
| 65 | + |
| 66 | + for locale in locales: |
| 67 | + cmd = ['pybabel', 'update', '-D', domain, |
| 68 | + '-i', os.path.join(path, '%s.pot' % domain), |
| 69 | + '-d', path, |
| 70 | + '-l', locale] |
| 71 | + print cmd |
| 72 | + call(cmd) |
0 commit comments