Changes between Initial Version and Version 1 of CookBookTemplateTags


Ignore:
Timestamp:
Feb 12, 2006, 3:47:14 AM (19 years ago)
Author:
g.brandl-nospam@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookTemplateTags

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