diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 14056d4759..70eb01d2d7 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -3,6 +3,7 @@ Internationalization support. """ from __future__ import unicode_literals import re +from django.utils.decorators import ContextDecorator from django.utils.encoding import force_text from django.utils.functional import lazy from django.utils import six @@ -149,7 +150,7 @@ def deactivate(): return _trans.deactivate() -class override(object): +class override(ContextDecorator): def __init__(self, language, deactivate=False): self.language = language self.deactivate = deactivate diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index ea5101ff72..fda8aa3d5d 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -129,6 +129,11 @@ Minor features * ... +:mod:``django.utils.translation`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +* ``django.utils.translation.override`` is now usable as a function decorator. + Cache ^^^^^ diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 0599b0b006..3ffcb08d8b 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -73,6 +73,25 @@ class TranslationTests(TestCase): finally: deactivate() + def test_override_decorator(self): + activate('de') + + @translation.override('pl') + def func_pl(): + self.assertEqual(get_language(), 'pl') + + @translation.override(None) + def func_none(): + self.assertEqual(get_language(), settings.LANGUAGE_CODE) + + try: + func_pl() + self.assertEqual(get_language(), 'de') + func_none() + self.assertEqual(get_language(), 'de') + finally: + deactivate() + def test_lazy_objects(self): """ Format string interpolation should work with *_lazy objects.