3 | | == Easy translation == |
4 | | |
5 | | This creates a new tag pair, {{{{% translate %}}}} and {{{{% endtranslate %}}}} |
6 | | which will translate every string between two backquotes. I made this |
7 | | because I found the default i18n "trans" tag too bulky. |
8 | | |
9 | | {{{ |
10 | | #!python |
11 | | import re |
12 | | from django.core import template |
13 | | from django.utils import translation |
14 | | |
15 | | register = template.Library() |
16 | | transl = re.compile("`(.*?)`") |
17 | | |
18 | | class 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.gettext(match.group(1)), output) |
25 | | |
26 | | |
27 | | def do_translate(parser, token): |
28 | | nodelist = parser.parse(('endtranslate',)) |
29 | | parser.delete_first_token() |
30 | | return TranslateNode(nodelist) |
31 | | |
32 | | register.tag('translate', do_translate) |
33 | | }}} |
| 2 | [wiki:CookBookTemplateTagTranslation] - Easy translation |