Skip to content

Commit

Permalink
Fixed #23323 -- Made django.utils.translation.override usable as a de…
Browse files Browse the repository at this point in the history
…corator.
  • Loading branch information
Thomas Chaumeny authored and charettes committed Aug 28, 2014
1 parent 191d953 commit 2db1ed1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion django/utils/translation/__init__.py
Expand Up @@ -3,6 +3,7 @@
"""
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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions docs/releases/1.8.txt
Expand Up @@ -129,6 +129,11 @@ Minor features

* ...

:mod:``django.utils.translation``

This comment has been minimized.

Copy link
@timgraham

timgraham Aug 29, 2014

Member

I think we should remove this note as this function is not documented. If not, the double backticks should be single and the underline length should match the heading length.

This comment has been minimized.

Copy link
@charettes

charettes Aug 29, 2014

Member

Completely overlooked this part, I thought it was part of the public API. Thanks for the admonition and the partial revert.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* ``django.utils.translation.override`` is now usable as a function decorator.

Cache
^^^^^

Expand Down
19 changes: 19 additions & 0 deletions tests/i18n/tests.py
Expand Up @@ -73,6 +73,25 @@ def test_override(self):
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.
Expand Down

0 comments on commit 2db1ed1

Please sign in to comment.