Fixed #23323 -- Made django.utils.translation.override usable as a decorator.

This commit is contained in:
Thomas Chaumeny 2014-08-19 18:44:10 +02:00 committed by Simon Charette
parent 191d953c99
commit 2db1ed1033
3 changed files with 26 additions and 1 deletions

View File

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

View File

@ -129,6 +129,11 @@ Minor features
* ...
:mod:``django.utils.translation``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ``django.utils.translation.override`` is now usable as a function decorator.
Cache
^^^^^

View File

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