Skip to content
Browse files

Merge pull request #4 from getlogic/master

Added the missing language templatetag so you can actually switch between languages in your templates (needs more tests)
  • Loading branch information...
2 parents 354a530 + d97ba91 commit e913c249cebdc5cd8edf521875af84d9ecfef7d4 @brocaar committed
Showing with 58 additions and 3 deletions.
  1. +3 −3 README.rst
  2. 0 i18nurls/templatetags/__init__.py
  3. +55 −0 i18nurls/templatetags/i18nurls.py
View
6 README.rst
@@ -12,8 +12,8 @@ the right language (based on the language-prefix in the requested URL).
functionality into the Django core. This patch was accepted and will be
included in Django 1.4 (thanks to Jannis Leidel and Russell Keith-Magee for
their feedback and reviewing the patch).
-
-
+
+
In the 0.6 version of this package, I rewrote the API so that it will match
with the upcoming Django 1.4 version. You can read more about this in the
`Django documentation (dev) <http://docs.djangoproject.com/en/dev/topics/i18n/internationalization/#specifying-translation-strings-in-url-patterns>`_.
@@ -26,8 +26,8 @@ After installing this package, URL patterns can also be marked translatable
using the ``ugettext_lazy()`` function. Example::
from django.conf.urls.defaults import patterns, include, url
- from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _
+ from i18nurls.i18n import i18n_patterns
urlpatterns = patterns(''
url(r'^sitemap\.xml$', 'sitemap.view', name='sitemap_xml'),
View
0 i18nurls/templatetags/__init__.py
No changes.
View
55 i18nurls/templatetags/i18nurls.py
@@ -0,0 +1,55 @@
+from django import template
+from django.utils import translation
+
+register = template.Library()
+
+
+class override(object):
+ def __init__(self, language, deactivate=False):
+ self.language = language
+ self.deactivate = deactivate
+ self.old_language = translation.get_language()
+
+ def __enter__(self):
+ if self.language is not None:
+ 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)
+
+
+class LanguageNode(template.Node):
+ def __init__(self, nodelist, language):
+ self.nodelist = nodelist
+ self.language = language
+
+ def render(self, context):
+ with override(self.language.resolve(context)):
+ output = self.nodelist.render(context)
+ return output
+
+
+@register.tag
+def language(parser, token):
+ """
+ This will enable the given language just for this block.
+
+ Usage::
+
+ {% language "de" %}
+ This is {{ bar }} and {{ boo }}.
+ {% endlanguage %}
+
+ """
+ bits = token.split_contents()
+ if len(bits) != 2:
+ raise template.TemplateSyntaxError("'%s' takes one argument (language)" % bits[0])
+ language = parser.compile_filter(bits[1])
+ nodelist = parser.parse(('endlanguage',))
+ parser.delete_first_token()
+ return LanguageNode(nodelist, language)

0 comments on commit e913c24

Please sign in to comment.
Something went wrong with that request. Please try again.