Browse Source

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.
Claude Paroz 10 years ago
parent
commit
543df07720

+ 3 - 0
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():

+ 8 - 1
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()
 

+ 3 - 0
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

+ 2 - 2
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')

+ 3 - 5
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