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.
This commit is contained in:
Claude Paroz 2015-01-08 11:11:35 +01:00
parent d6c8121ed0
commit 543df07720
5 changed files with 19 additions and 8 deletions

View File

@ -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():

View File

@ -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()

View File

@ -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

View File

@ -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')

View File

@ -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