Context Navigation


CookBook - Template tags

Easy translation

This creates a new tag pair, {% translate %} and {% endtranslate %} which will translate every string between two backquotes. I made this because I found the default i18n "trans" tag too bulky.

importre
fromdjango.coreimport template
fromdjango.utilsimport translation
register = template.Library()
transl = re.compile("`(.*?)`")
classTranslateNode(template.Node):
 def__init__(self, nodelist):
 self.nodelist = nodelist
 defrender(self, context):
 output = self.nodelist.render(context)
 return transl.sub(lambda match: translation.gettext(match.group(1)), output)
defdo_translate(parser, token):
 nodelist = parser.parse(('endtranslate',))
 parser.delete_first_token()
 return TranslateNode(nodelist)
register.tag('translate', do_translate)

Can you explain the difference between:

{% blocktrans %}
 asd adlkjfjalkdsfj lksajdfl sdkf jslakdfj
 asd adlkjfjalkdsfj lksajdfl sdkf jslakdfj
 asd adlkjfjalkdsfj lksajdfl sdkf jslakdfj
{% endblocktrans %}

and your method?

{% translate %}
 `saddasdasd asd ad asd`
 `saddasdasd asd ad asd`
{% endtranslate %}

Also, your tags are not picked up with make-messages so you must insert them manually into .po files.

Last modified 20 years ago Last modified on Apr 6, 2006, 2:27:50 AM
Note: See TracWiki for help on using the wiki.
Back to Top