From 543df07720181fe23737ba14f1a261ff6f37f49c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 8 Jan 2015 11:11:35 +0100 Subject: [PATCH] Fixed #24073 -- Returned None for get_language when translations are deactivated This fixes a regression caused by f7c287fca9. Thanks Markus Holtermann for identifying the regression. --- django/utils/translation/trans_real.py | 3 +++ docs/ref/utils.txt | 9 ++++++++- docs/releases/1.8.txt | 3 +++ tests/i18n/tests.py | 4 ++-- tests/user_commands/tests.py | 8 +++----- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 1d4d100206..4e6a359df2 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -211,6 +211,8 @@ def activate(language): Fetches the translation object for a given language and installs it as the current translation object for the current thread. """ + if not language: + return if language in _DJANGO_DEPRECATED_LOCALES: msg = ("The use of the language code '%s' is deprecated. " "Please use the '%s' translation instead.") @@ -235,6 +237,7 @@ def deactivate_all(): for some reason. """ _active.value = gettext_module.NullTranslations() + _active.value.to_language = lambda *args: None def get_language(): diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index 327f44bbfc..195647ea15 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -1098,7 +1098,14 @@ For a complete discussion on the usage of the following see the .. function:: get_language() - Returns the currently selected language code. + Returns the currently selected language code. Returns ``None`` if + translations are temporarily deactivated (by :func:`deactivate_all()` or + when ``None`` is passed to :func:`override()`). + + .. versionchanged:: 1.8 + + Before Django 1.8, ``get_language()`` always returned + :setting:`LANGUAGE_CODE` when translations were deactivated. .. function:: get_language_bidi() diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index cb25a7e8b3..ddcb9a992d 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -1021,6 +1021,9 @@ Miscellaneous this will not happen any longer. It might be that new database migrations are generated (once) after migrating to 1.8. +* :func:`django.utils.translation.get_language()` now returns ``None`` instead + of :setting:`LANGUAGE_CODE` when translations are temporarily deactivated. + .. _deprecated-features-1.8: Features deprecated in 1.8 diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 9e271b3fa6..a0df8daddf 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -68,7 +68,7 @@ class TranslationTests(TestCase): self.assertEqual(get_language(), 'pl') self.assertEqual(get_language(), 'de') with translation.override(None): - self.assertEqual(get_language(), settings.LANGUAGE_CODE) + self.assertEqual(get_language(), None) self.assertEqual(get_language(), 'de') finally: deactivate() @@ -81,7 +81,7 @@ class TranslationTests(TestCase): @translation.override(None) def func_none(): - self.assertEqual(get_language(), settings.LANGUAGE_CODE) + self.assertEqual(get_language(), None) try: activate('de') diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index c364b868a3..40ffca95cb 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -2,7 +2,6 @@ import os from django.apps import apps from django.db import connection -from django.conf import settings from django.core import management from django.core.management import BaseCommand, CommandError, find_commands from django.core.management.utils import find_command, popen_wrapper @@ -48,13 +47,12 @@ class CommandTests(SimpleTestCase): management.ManagementUtility(['manage.py', 'dance', '--example=raise']).execute() self.assertIn("CommandError", stderr.getvalue()) - def test_default_en_us_locale_set(self): - # Forces en_us when set to true + def test_deactivate_locale_set(self): + # Deactivate translation when set to true out = StringIO() with translation.override('pl'): management.call_command('leave_locale_alone_false', stdout=out) - # get_language returns settings.LANGUAGE_CODE for NullTranslations instances - self.assertEqual(out.getvalue(), "%s\n" % settings.LANGUAGE_CODE) + self.assertEqual(out.getvalue(), "") def test_configured_locale_preserved(self): # Leaves locale from settings when set to false