Commit 0707b824 authored by Ana Krivokapic's avatar Ana Krivokapic Committed by Loic Bistuer
Browse files

Fixed #22328 -- Added --exclude option to compilemessages and makemessages.

parent d1f93e9c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -367,6 +367,7 @@ answer newbie questions, and generally made Django that much better:
    Martin Kosír <martin@martinkosir.net>
    Arthur Koziel <http://arthurkoziel.com>
    Meir Kriheli <http://mksoft.co.il/>
    Ana Krivokapic <https://github.com/infraredgirl>
    Bruce Kroeze <http://coderseye.com/>
    krzysiek.pawlik@silvermedia.pl
    konrad@gwu.edu
+21 −6
Original line number Diff line number Diff line
from __future__ import unicode_literals

import codecs
import glob
import os
from optparse import make_option

@@ -30,8 +31,11 @@ def is_writable(path):

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--locale', '-l', dest='locale', action='append',
                    help='locale(s) to process (e.g. de_AT). Default is to process all. Can be used multiple times.'),
        make_option('--locale', '-l', dest='locale', action='append', default=[],
                    help='Locale(s) to process (e.g. de_AT). Default is to process all. Can be '
                         'used multiple times.'),
        make_option('--exclude', '-e', dest='exclude', action='append', default=[],
                    help='Locales to exclude. Default is none. Can be used multiple times.'),
    )
    help = 'Compiles .po files to .mo files for use with builtin gettext support.'

@@ -43,6 +47,7 @@ class Command(BaseCommand):

    def handle(self, **options):
        locale = options.get('locale')
        exclude = options.get('exclude')
        self.verbosity = int(options.get('verbosity'))

        if find_command(self.program) is None:
@@ -62,9 +67,19 @@ class Command(BaseCommand):
                               "checkout or your project or app tree, or with "
                               "the settings module specified.")

        # Build locale list
        all_locales = []
        for basedir in basedirs:
            if locale:
                dirs = [os.path.join(basedir, l, 'LC_MESSAGES') for l in locale]
            locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % basedir))
            all_locales.extend(map(os.path.basename, locale_dirs))

        # Account for excluded locales
        locales = locale or all_locales
        locales = set(locales) - set(exclude)

        for basedir in basedirs:
            if locales:
                dirs = [os.path.join(basedir, l, 'LC_MESSAGES') for l in locales]
            else:
                dirs = [basedir]
            locations = []
@@ -90,8 +105,8 @@ class Command(BaseCommand):

            # Check writability on first location
            if i == 0 and not is_writable(npath(base_path + '.mo')):
                self.stderr.write("The po files under %s are in a seemingly not "
                                  "writable location. mo files will not be updated/created." % dirpath)
                self.stderr.write("The po files under %s are in a seemingly not writable location. "
                                  "mo files will not be updated/created." % dirpath)
                return

            args = [self.program] + self.program_options + ['-o',
+16 −9
Original line number Diff line number Diff line
@@ -160,9 +160,11 @@ def write_pot_file(potfile, msgs):

class Command(NoArgsCommand):
    option_list = NoArgsCommand.option_list + (
        make_option('--locale', '-l', default=None, dest='locale', action='append',
        make_option('--locale', '-l', default=[], dest='locale', action='append',
            help='Creates or updates the message files for the given locale(s) (e.g. pt_BR). '
                 'Can be used multiple times.'),
        make_option('--exclude', '-e', default=[], dest='exclude', action='append',
                    help='Locales to exclude. Default is none. Can be used multiple times.'),
        make_option('--domain', '-d', default='django', dest='domain',
            help='The domain of the message files (default: "django").'),
        make_option('--all', '-a', action='store_true', dest='all',
@@ -189,7 +191,7 @@ class Command(NoArgsCommand):
"pulls out all strings marked for translation. It creates (or updates) a message "
"file in the conf/locale (in the django tree) or locale (for projects and "
"applications) directory.\n\nYou must run this command with one of either the "
"--locale or --all options.")
"--locale, --exclude or --all options.")

    requires_system_checks = False
    leave_locale_alone = True
@@ -201,6 +203,7 @@ class Command(NoArgsCommand):

    def handle_noargs(self, *args, **options):
        locale = options.get('locale')
        exclude = options.get('exclude')
        self.domain = options.get('domain')
        self.verbosity = int(options.get('verbosity'))
        process_all = options.get('all')
@@ -235,7 +238,7 @@ class Command(NoArgsCommand):
            exts = extensions if extensions else ['html', 'txt']
        self.extensions = handle_extensions(exts)

        if (locale is None and not process_all) or self.domain is None:
        if (locale is None and not exclude and not process_all) or self.domain is None:
            raise CommandError("Type '%s help %s' for usage information." % (
                os.path.basename(sys.argv[0]), sys.argv[1]))

@@ -270,12 +273,16 @@ class Command(NoArgsCommand):
                    os.makedirs(self.default_locale_path)

        # Build locale list
        locales = []
        if locale is not None:
            locales = locale
        elif process_all:
        locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % self.default_locale_path))
            locales = [os.path.basename(l) for l in locale_dirs]
        all_locales = map(os.path.basename, locale_dirs)

        # Account for excluded locales
        if process_all:
            locales = all_locales
        else:
            locales = locale or all_locales
            locales = set(locales) - set(exclude)

        if locales:
            check_programs('msguniq', 'msgmerge', 'msgattrib')

+5 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ script found at the top level of each Django project directory.
.BI cleanup
Cleans out old data from the database (only expired sessions at the moment).
.TP
.BI "compilemessages [" "\-\-locale=LOCALE" "]"
.BI "compilemessages [" "\-\-locale=LOCALE" "] [" "\-\-exclude=LOCALE" "]"
Compiles .po files to .mo files for use with builtin gettext support.
.TP
.BI "createcachetable [" "tablename" "]"
@@ -59,7 +59,7 @@ Executes
.B sqlall
for the given app(s) in the current database.
.TP
.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "] [" "\-\-no\-wrap" "] [" "\-\-no\-location" "]"
.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-exclude=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "] [" "\-\-no\-wrap" "] [" "\-\-no\-location" "]"
Runs over the entire source tree of the current directory and pulls out all
strings marked for translation. It creates (or updates) a message file in the
conf/locale (in the django tree) or locale (for project and application) directory.
@@ -176,6 +176,9 @@ output a full stack trace whenever an exception is raised.
.I \-l, \-\-locale=LOCALE
The locale to process when using makemessages or compilemessages.
.TP
.I \-e, \-\-exclude=LOCALE
The locale to exclude from processing when using makemessages or compilemessages.
.TP
.I \-d, \-\-domain=DOMAIN
The domain of the message files (default: "django") when using makemessages.
.TP
+21 −0
Original line number Diff line number Diff line
@@ -141,12 +141,22 @@ the builtin gettext support. See :doc:`/topics/i18n/index`.
Use the :djadminopt:`--locale` option (or its shorter version ``-l``) to
specify the locale(s) to process. If not provided, all locales are processed.

.. versionadded:: 1.8

Use the :djadminopt:`--exclude` option (or its shorter version ``-e``) to
specify the locale(s) to exclude from processing. If not provided, no locales
are excluded.

Example usage::

    django-admin.py compilemessages --locale=pt_BR
    django-admin.py compilemessages --locale=pt_BR --locale=fr
    django-admin.py compilemessages -l pt_BR
    django-admin.py compilemessages -l pt_BR -l fr
    django-admin.py compilemessages --exclude=pt_BR
    django-admin.py compilemessages --exclude=pt_BR --exclude=fr
    django-admin.py compilemessages -e pt_BR
    django-admin.py compilemessages -e pt_BR -e fr

createcachetable
----------------
@@ -551,12 +561,23 @@ Separate multiple extensions with commas or use -e or --extension multiple times
Use the :djadminopt:`--locale` option (or its shorter version ``-l``) to
specify the locale(s) to process.

.. versionadded:: 1.8

Use the :djadminopt:`--exclude` option (or its shorter version ``-e``) to
specify the locale(s) to exclude from processing. If not provided, no locales
are excluded.

Example usage::

    django-admin.py makemessages --locale=pt_BR
    django-admin.py makemessages --locale=pt_BR --locale=fr
    django-admin.py makemessages -l pt_BR
    django-admin.py makemessages -l pt_BR -l fr
    django-admin.py makemessages --exclude=pt_BR
    django-admin.py makemessages --exclude=pt_BR --exclude=fr
    django-admin.py makemessages -e pt_BR
    django-admin.py makemessages -e pt_BR -e fr


.. versionchanged:: 1.7

Loading