diff options
| author | Thomas Chaumeny <thomas.chaumeny@polyconseil.fr> | 2014-08-19 18:44:10 +0200 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2014-08-28 11:58:21 -0400 |
| commit | 2db1ed1033c0f542b3ffcbbb970d1b4dc881900f (patch) | |
| tree | 7ea8b0c3ef6e8064078790ba6c83c73ada9696f1 | |
| parent | 191d953c99b2b437370612fb63b1247a4f543e5e (diff) | |
Fixed #23323 -- Made django.utils.translation.override usable as a decorator.
| -rw-r--r-- | django/utils/translation/__init__.py | 3 | ||||
| -rw-r--r-- | docs/releases/1.8.txt | 5 | ||||
| -rw-r--r-- | tests/i18n/tests.py | 19 |
3 files changed, 26 insertions, 1 deletions
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. |
