diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 246936e403..7311a6145c 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -164,7 +164,9 @@ class override(ContextDecorator): deactivate_all() def __exit__(self, exc_type, exc_value, traceback): - if self.deactivate: + if self.old_language is None: + deactivate_all() + elif self.deactivate: deactivate() else: activate(self.old_language) diff --git a/docs/releases/1.8.5.txt b/docs/releases/1.8.5.txt index d1e75d97d6..3f1674b979 100644 --- a/docs/releases/1.8.5.txt +++ b/docs/releases/1.8.5.txt @@ -13,3 +13,5 @@ Bugfixes field that is both a foreign and primary key (:ticket:`24951`). * Fixed a migrations crash with ``GenericForeignKey`` (:ticket:`25040`). +* Made ``translation.override()`` clear the overridden language when a + translation isn't initially active (:ticket:`25295`). diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index a4e6fc9fc2..00e9bf85d9 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -76,6 +76,9 @@ class TranslationTests(SimpleTestCase): self.assertEqual(get_language(), 'de') with translation.override(None): self.assertEqual(get_language(), None) + with translation.override('pl'): + pass + self.assertEqual(get_language(), None) self.assertEqual(get_language(), 'de') finally: deactivate()