Changes between Initial Version and Version 1 of CookBookTemplateTagTranslation


Ignore:
Timestamp:
Feb 27, 2006, 4:47:38 AM (18 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookTemplateTagTranslation

    v1 v1  
     1
     2= CookBook - Template tags =
     3
     4== Easy translation ==
     5
     6This creates a new tag pair, {{{{% translate %}}}} and {{{{% endtranslate %}}}}
     7which will translate every string between two backquotes. I made this
     8because I found the default i18n "trans" tag too bulky.
     9
     10{{{
     11#!python
     12import re
     13from django.core import template
     14from django.utils import translation
     15
     16register = template.Library()
     17transl = re.compile("`(.*?)`")
     18
     19class TranslateNode(template.Node):
     20    def __init__(self, nodelist):
     21        self.nodelist = nodelist
     22
     23    def render(self, context):
     24        output = self.nodelist.render(context)
     25        return transl.sub(lambda match: translation.gettext(match.group(1)), output)
     26
     27
     28def do_translate(parser, token):
     29    nodelist = parser.parse(('endtranslate',))
     30    parser.delete_first_token()
     31    return TranslateNode(nodelist)
     32
     33register.tag('translate', do_translate)
     34}}}
Back to Top