|
| 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( |
| 17 | + '--locale', '-l', |
| 18 | + default=None, dest='locale', action='append', |
| 19 | + help='Creates or updates the message files for the given locale(s)' |
| 20 | + ' (e.g pt_BR). Can be used multiple times.'), |
| 21 | + make_option('--domain', '-d', |
| 22 | + default='django', dest='domain', |
| 23 | + help='The domain of the message files (default: "django").'), |
| 24 | + make_option('--mapping-file', '-F', |
| 25 | + default=None, dest='mapping_file', |
| 26 | + help='Mapping file') |
| 27 | + ) |
| 28 | + |
| 29 | + def handle_label(self, command, **options): |
| 30 | + if command not in ('makemessages', 'compilemessages'): |
| 31 | + raise CommandError( |
| 32 | + "You must either apply 'makemessages' or 'compilemessages'" |
| 33 | + ) |
| 34 | + |
| 35 | + if command == 'makemessages': |
| 36 | + self.handle_makemessages(**options) |
| 37 | + if command == 'compilemessages': |
| 38 | + self.handle_compilemessages(**options) |
| 39 | + |
| 40 | + def handle_makemessages(self, **options): |
| 41 | + locale_paths = list(settings.LOCALE_PATHS) |
| 42 | + domain = options.pop('domain') |
| 43 | + locales = options.pop('locale') |
| 44 | + |
| 45 | + # support for mapping file specification via setup.cfg |
| 46 | + # TODO: Try to support all possible options. |
| 47 | + distribution = Distribution() |
| 48 | + distribution.parse_config_files(distribution.find_config_files()) |
| 49 | + |
| 50 | + mapping_file = options.pop('mapping_file', None) |
| 51 | + has_extract = 'extract_messages' in distribution.command_options |
| 52 | + if mapping_file is None and has_extract: |
| 53 | + opts = distribution.command_options['extract_messages'] |
| 54 | + try: |
| 55 | + mapping_file = opts['mapping_file'][1] |
| 56 | + except (IndexError, KeyError): |
| 57 | + mapping_file = None |
| 58 | + |
| 59 | + for path in locale_paths: |
| 60 | + potfile = os.path.join(path, '%s.pot' % domain) |
| 61 | + if not os.path.exists(potfile): |
| 62 | + continue |
| 63 | + |
| 64 | + cmd = ['pybabel', 'extract', '-o', potfile] |
| 65 | + |
| 66 | + if mapping_file is not None: |
| 67 | + cmd.extend(['-F', mapping_file]) |
| 68 | + |
| 69 | + cmd.append(os.path.dirname(os.path.relpath(path))) |
| 70 | + |
| 71 | + call(cmd) |
| 72 | + |
| 73 | + for locale in locales: |
| 74 | + cmd = ['pybabel', 'update', '-D', domain, |
| 75 | + '-i', potfile, |
| 76 | + '-d', os.path.relpath(path), |
| 77 | + '-l', locale] |
| 78 | + call(cmd) |
| 79 | + |
| 80 | + def handle_compilemessages(self, **options): |
| 81 | + locale_paths = list(settings.LOCALE_PATHS) |
| 82 | + domain = options.pop('domain') |
| 83 | + locales = options.pop('locale') |
| 84 | + |
| 85 | + for path in locale_paths: |
| 86 | + for locale in locales: |
| 87 | + po_file = os.path.join( |
| 88 | + path, locale, 'LC_MESSAGES', domain + '.po' |
| 89 | + ) |
| 90 | + if os.path.exists(po_file): |
| 91 | + cmd = ['pybabel', 'compile', '-D', domain, |
| 92 | + '-d', path, '-l', locale] |
| 93 | + call(cmd) |
0 commit comments