mirror of
https://github.com/django/django.git
synced 2025-06-02 18:19:11 +00:00
Fixed #23323 -- Made django.utils.translation.override usable as a decorator.
This commit is contained in:
parent
191d953c99
commit
2db1ed1033
@ -3,6 +3,7 @@ Internationalization support.
|
|||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import re
|
import re
|
||||||
|
from django.utils.decorators import ContextDecorator
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.utils.functional import lazy
|
from django.utils.functional import lazy
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
@ -149,7 +150,7 @@ def deactivate():
|
|||||||
return _trans.deactivate()
|
return _trans.deactivate()
|
||||||
|
|
||||||
|
|
||||||
class override(object):
|
class override(ContextDecorator):
|
||||||
def __init__(self, language, deactivate=False):
|
def __init__(self, language, deactivate=False):
|
||||||
self.language = language
|
self.language = language
|
||||||
self.deactivate = deactivate
|
self.deactivate = deactivate
|
||||||
|
@ -129,6 +129,11 @@ Minor features
|
|||||||
|
|
||||||
* ...
|
* ...
|
||||||
|
|
||||||
|
:mod:``django.utils.translation``
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
* ``django.utils.translation.override`` is now usable as a function decorator.
|
||||||
|
|
||||||
Cache
|
Cache
|
||||||
^^^^^
|
^^^^^
|
||||||
|
|
||||||
|
@ -73,6 +73,25 @@ class TranslationTests(TestCase):
|
|||||||
finally:
|
finally:
|
||||||
deactivate()
|
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):
|
def test_lazy_objects(self):
|
||||||
"""
|
"""
|
||||||
Format string interpolation should work with *_lazy objects.
|
Format string interpolation should work with *_lazy objects.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user