From e92b3b723f9bf953d0465e92e444045cdc837042 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Mon, 7 Nov 2011 11:28:31 +0000 Subject: [PATCH] Fixed #11118 -- Made management command BaseCommand restore locale after execution. Thanks rvdrijst for the report and initial patch and Claude Paroz for enhancing it. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17077 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/base.py | 4 ++++ tests/modeltests/user_commands/tests.py | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/core/management/base.py b/django/core/management/base.py index 77c80fe695..d2dd266527 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -203,9 +203,11 @@ class BaseCommand(object): # like permissions, and those shouldn't contain any translations. # But only do this if we can assume we have a working settings file, # because django.utils.translation requires settings. + saved_lang = None if self.can_import_settings: try: from django.utils import translation + saved_lang = translation.get_language() translation.activate('en-us') except ImportError, e: # If settings should be available, but aren't, @@ -232,6 +234,8 @@ class BaseCommand(object): except CommandError, e: self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e))) sys.exit(1) + if saved_lang is not None: + translation.activate(saved_lang) def validate(self, app=None, display_num_errors=False): """ diff --git a/tests/modeltests/user_commands/tests.py b/tests/modeltests/user_commands/tests.py index bf74a98440..896dd667ff 100644 --- a/tests/modeltests/user_commands/tests.py +++ b/tests/modeltests/user_commands/tests.py @@ -3,6 +3,7 @@ from StringIO import StringIO from django.core import management from django.core.management.base import CommandError from django.test import TestCase +from django.utils import translation class CommandTests(TestCase): @@ -18,5 +19,11 @@ class CommandTests(TestCase): self.assertEqual(out.getvalue(), "I don't feel like dancing Jive.") + def test_language_preserved(self): + out = StringIO() + with translation.override('fr'): + management.call_command('dance', stdout=out) + self.assertEqual(translation.get_language(), 'fr') + def test_explode(self): - self.assertRaises(CommandError, management.call_command, ('explode',)) \ No newline at end of file + self.assertRaises(CommandError, management.call_command, ('explode',))