Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test added for override context manager.

  • Loading branch information...
commit ca920f7fdf4b945e80063facf184ae53c0b2c2c4 1 parent 082651c
@brocaar authored
View
14 i18nurls/templatetags/i18nurls.py
@@ -1,26 +1,23 @@
from django import template
from django.utils import translation
+
register = template.Library()
class override(object):
- def __init__(self, language, deactivate=False):
+ def __init__(self, language):
self.language = language
- self.deactivate = deactivate
self.old_language = translation.get_language()
def __enter__(self):
- if self.language is not None:
+ if self.language:
translation.activate(self.language)
else:
translation.deactivate_all()
def __exit__(self, exc_type, exc_value, traceback):
- if self.deactivate:
- translation.deactivate()
- else:
- translation.activate(self.old_language)
+ translation.activate(self.old_language)
class LanguageNode(template.Node):
@@ -48,7 +45,8 @@ def language(parser, token):
"""
bits = token.split_contents()
if len(bits) != 2:
- raise template.TemplateSyntaxError("'%s' takes one argument (language)" % bits[0])
+ raise template.TemplateSyntaxError(
+ "'%s' takes one argument (language)" % bits[0])
language = parser.compile_filter(bits[1])
nodelist = parser.parse(('endlanguage',))
parser.delete_first_token()
View
1  i18nurls/tests/__init__.py
@@ -1 +1,2 @@
from i18nurls.tests.url_tests import *
+from i18nurls.tests.templatetags_tests import *
View
15 i18nurls/tests/templatetags_tests.py
@@ -0,0 +1,15 @@
+from django.utils import translation
+from i18nurls.templatetags.i18nurls import override
+from i18nurls.tests.base import BaseTestCase
+
+
+class OverrideTest(BaseTestCase):
+
+ def test_override(self):
+ translation.activate('en')
+ self.assertEqual('en', translation.get_language())
+
+ with override('nl'):
+ self.assertEqual('nl', translation.get_language())
+
+ self.assertEqual('en', translation.get_language())
Please sign in to comment.
Something went wrong with that request. Please try again.