Ticket #17379: 17379-4.diff

File 17379-4.diff, 7.0 KB (added by gabooo, 12 years ago)

Fix i18n makemessages command

  • django/core/management/base.py

    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index db855e1..6be57d8 100644
    a b class BaseCommand(object):  
    137137
    138138    # Configuration shortcuts that alter various logic.
    139139    can_import_settings = True
     140    force_en_us_locale = True
    140141    requires_model_validation = True
    141142    output_transaction = False  # Whether to wrap the output in a "BEGIN; COMMIT;"
    142143
    class BaseCommand(object):  
    204205        stderr.
    205206        """
    206207        show_traceback = options.get('traceback', False)
    207 
    208         # Switch to English, because django-admin.py creates database content
    209         # like permissions, and those shouldn't contain any translations.
    210         # But only do this if we can assume we have a working settings file,
    211         # because django.utils.translation requires settings.
    212         saved_lang = None
     208        self.stdout = options.get('stdout', sys.stdout)
     209        self.stderr = options.get('stderr', sys.stderr)
    213210        if self.can_import_settings:
    214211            try:
    215                 from django.utils import translation
    216                 saved_lang = translation.get_language()
    217                 translation.activate('en-us')
     212                from django.conf import settings
     213                settings.__getattr__('_', None)
    218214            except ImportError, e:
    219215                # If settings should be available, but aren't,
    220216                # raise the error and quit.
    class BaseCommand(object):  
    223219                else:
    224220                    sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
    225221                sys.exit(1)
    226 
     222               
     223               
     224        # Switch to English, because django-admin.py creates database content
     225        # like permissions, and those shouldn't contain any translations.
     226        # But only do this if we can assume we have a working settings file,
     227        # because django.utils.translation requires settings.
     228        saved_lang = None
     229        if self.force_en_us_locale:
     230            if not self.can_import_settings:
     231                raise StandardError("Can't force locale with can_import_settings set to False")
     232            from django.utils import translation
     233            saved_lang = translation.get_language()
     234            translation.activate('en-us')
     235            if int(options.get('verbosity', 1)) >= 2:
     236                self.stdout.write("Active language set to 'en-us'.\n")
     237               
    227238        try:
    228             self.stdout = options.get('stdout', sys.stdout)
    229             self.stderr = options.get('stderr', sys.stderr)
    230239            if self.requires_model_validation:
    231240                self.validate()
    232241            output = self.handle(*args, **options)
  • django/core/management/commands/makemessages.py

    diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
    index 95c1da0..4478ca4 100644
    a b class Command(NoArgsCommand):  
    364364"--locale or --all options.")
    365365
    366366    requires_model_validation = False
    367     can_import_settings = False
     367    force_en_us_locale = False
    368368
    369369    def handle_noargs(self, *args, **options):
    370370        locale = options.get('locale')
  • django/core/management/commands/shell.py

    diff --git a/django/core/management/commands/shell.py b/django/core/management/commands/shell.py
    index 26cbd7f..902a4fd 100644
    a b class Command(NoArgsCommand):  
    1010    help = "Runs a Python interactive interpreter. Tries to use IPython, if it's available."
    1111    shells = ['ipython', 'bpython']
    1212    requires_model_validation = False
     13    force_en_us_locale = False
    1314
    1415    def ipython(self):
    1516        try:
  • new file tests/modeltests/user_commands/management/commands/force_en_us_locale_false.py

    diff --git a/tests/modeltests/user_commands/management/commands/force_en_us_locale_false.py b/tests/modeltests/user_commands/management/commands/force_en_us_locale_false.py
    new file mode 100644
    index 0000000..7bae951
    - +  
     1from django.core.management.base import BaseCommand
     2from django.utils import translation
     3
     4class Command(BaseCommand):
     5    can_import_settings=True
     6    force_en_us_locale=False
     7    def handle(self, *args, **options):
     8        return translation.get_language()
  • new file tests/modeltests/user_commands/management/commands/force_en_us_locale_true.py

    diff --git a/tests/modeltests/user_commands/management/commands/force_en_us_locale_true.py b/tests/modeltests/user_commands/management/commands/force_en_us_locale_true.py
    new file mode 100644
    index 0000000..0c49eb2
    - +  
     1from django.core.management.base import BaseCommand
     2from django.utils import translation
     3
     4class Command(BaseCommand):
     5    can_import_settings=True
     6    force_en_us_locale=True
     7    def handle(self, *args, **options):
     8        return translation.get_language()
  • tests/modeltests/user_commands/tests.py

    diff --git a/tests/modeltests/user_commands/tests.py b/tests/modeltests/user_commands/tests.py
    index e138e07..d7fc35c 100644
    a b class CommandTests(TestCase):  
    2323
    2424    def test_language_preserved(self):
    2525        out = StringIO()
    26         with translation.override('fr'):
     26        with translation.override('pl'):
    2727            management.call_command('dance', stdout=out)
    28             self.assertEqual(translation.get_language(), 'fr')
     28            self.assertEqual(translation.get_language(), 'pl')
    2929
    3030    def test_explode(self):
    3131        self.assertRaises(CommandError, management.call_command, ('explode',))
     32       
     33        # Forces en_us when set to true
     34        out = StringIO()
     35        with translation.override('pl'):
     36            management.call_command('force_en_us_locale_true', stdout=out)
     37            self.assertEqual(out.getvalue(), "en-us")       
     38
     39        # Leaves locale from settings when set to false
     40        out = StringIO()
     41        with translation.override('pl'):
     42            management.call_command('force_en_us_locale_false', stdout=out)
     43            self.assertEqual(out.getvalue(), "pl")
  • tests/regressiontests/i18n/commands/extraction.py

    diff --git a/tests/regressiontests/i18n/commands/extraction.py b/tests/regressiontests/i18n/commands/extraction.py
    index b1fc002..684c1a5 100644
    a b class BasicExtractorTests(ExtractorTests):  
    104104            self.assertMsgId('I think that 100%% is more that 50%% of %(obj)s.', po_contents)
    105105            self.assertMsgId("Blocktrans extraction shouldn't double escape this: %%, a=%(a)s", po_contents)
    106106
     107    def test_force_en_us_locale(self):
     108        from django.core.management.commands.makemessages import Command
     109        self.assertFalse(Command.force_en_us_locale)
     110       
    107111    def test_extraction_error(self):
    108112        os.chdir(self.test_dir)
    109113        shutil.copyfile('./templates/template_with_error.tpl', './templates/template_with_error.html')
Back to Top