diff --git a/django/core/management/base.py b/django/core/management/base.py
index db855e1..6be57d8 100644
|
a
|
b
|
class BaseCommand(object):
|
| 137 | 137 | |
| 138 | 138 | # Configuration shortcuts that alter various logic. |
| 139 | 139 | can_import_settings = True |
| | 140 | force_en_us_locale = True |
| 140 | 141 | requires_model_validation = True |
| 141 | 142 | output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;" |
| 142 | 143 | |
| … |
… |
class BaseCommand(object):
|
| 204 | 205 | stderr. |
| 205 | 206 | """ |
| 206 | 207 | 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) |
| 213 | 210 | if self.can_import_settings: |
| 214 | 211 | 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) |
| 218 | 214 | except ImportError, e: |
| 219 | 215 | # If settings should be available, but aren't, |
| 220 | 216 | # raise the error and quit. |
| … |
… |
class BaseCommand(object):
|
| 223 | 219 | else: |
| 224 | 220 | sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e))) |
| 225 | 221 | 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 | |
| 227 | 238 | try: |
| 228 | | self.stdout = options.get('stdout', sys.stdout) |
| 229 | | self.stderr = options.get('stderr', sys.stderr) |
| 230 | 239 | if self.requires_model_validation: |
| 231 | 240 | self.validate() |
| 232 | 241 | output = self.handle(*args, **options) |
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):
|
| 364 | 364 | "--locale or --all options.") |
| 365 | 365 | |
| 366 | 366 | requires_model_validation = False |
| 367 | | can_import_settings = False |
| | 367 | force_en_us_locale = False |
| 368 | 368 | |
| 369 | 369 | def handle_noargs(self, *args, **options): |
| 370 | 370 | locale = options.get('locale') |
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):
|
| 10 | 10 | help = "Runs a Python interactive interpreter. Tries to use IPython, if it's available." |
| 11 | 11 | shells = ['ipython', 'bpython'] |
| 12 | 12 | requires_model_validation = False |
| | 13 | force_en_us_locale = False |
| 13 | 14 | |
| 14 | 15 | def ipython(self): |
| 15 | 16 | try: |
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
|
-
|
+
|
|
| | 1 | from django.core.management.base import BaseCommand |
| | 2 | from django.utils import translation |
| | 3 | |
| | 4 | class Command(BaseCommand): |
| | 5 | can_import_settings=True |
| | 6 | force_en_us_locale=False |
| | 7 | def handle(self, *args, **options): |
| | 8 | return translation.get_language() |
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
|
-
|
+
|
|
| | 1 | from django.core.management.base import BaseCommand |
| | 2 | from django.utils import translation |
| | 3 | |
| | 4 | class Command(BaseCommand): |
| | 5 | can_import_settings=True |
| | 6 | force_en_us_locale=True |
| | 7 | def handle(self, *args, **options): |
| | 8 | return translation.get_language() |
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):
|
| 23 | 23 | |
| 24 | 24 | def test_language_preserved(self): |
| 25 | 25 | out = StringIO() |
| 26 | | with translation.override('fr'): |
| | 26 | with translation.override('pl'): |
| 27 | 27 | management.call_command('dance', stdout=out) |
| 28 | | self.assertEqual(translation.get_language(), 'fr') |
| | 28 | self.assertEqual(translation.get_language(), 'pl') |
| 29 | 29 | |
| 30 | 30 | def test_explode(self): |
| 31 | 31 | 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") |
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):
|
| 104 | 104 | self.assertMsgId('I think that 100%% is more that 50%% of %(obj)s.', po_contents) |
| 105 | 105 | self.assertMsgId("Blocktrans extraction shouldn't double escape this: %%, a=%(a)s", po_contents) |
| 106 | 106 | |
| | 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 | |
| 107 | 111 | def test_extraction_error(self): |
| 108 | 112 | os.chdir(self.test_dir) |
| 109 | 113 | shutil.copyfile('./templates/template_with_error.tpl', './templates/template_with_error.html') |