|
| 1 | +"""Modified Django templatize utility to translate Django L10n into something almost usable by Jinja2.""" |
| 2 | + |
| 3 | +from cStringIO import StringIO |
| 4 | +import re |
| 5 | + |
| 6 | + |
| 7 | +dot_re = re.compile(r'\S') |
| 8 | +def blankout(src, char): |
| 9 | + """ |
| 10 | + Changes every non-whitespace character to the given char. |
| 11 | + Used in the templatize function. |
| 12 | + """ |
| 13 | + return dot_re.sub(char, src) |
| 14 | + |
| 15 | +inline_re = re.compile(r"""^\s*trans\s+((?:".*?")|(?:'.*?'))\s*""") |
| 16 | +block_re = re.compile(r"""^\s*blocktrans(?:\s+|$)""") |
| 17 | +endblock_re = re.compile(r"""^\s*endblocktrans$""") |
| 18 | +plural_re = re.compile(r"""^\s*plural$""") |
| 19 | +constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""") |
| 20 | + |
| 21 | +def templatize(src): |
| 22 | + """ |
| 23 | + Turns a Django template into something that is understood by xgettext. It |
| 24 | + does so by translating the Django translation tags into standard gettext |
| 25 | + function invocations. |
| 26 | + """ |
| 27 | + from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK |
| 28 | + out = StringIO() |
| 29 | + intrans = False |
| 30 | + inplural = False |
| 31 | + singular = [] |
| 32 | + plural = [] |
| 33 | + for t in Lexer(src, None).tokenize(): |
| 34 | + if intrans: |
| 35 | + if t.token_type == TOKEN_BLOCK: |
| 36 | + endbmatch = endblock_re.match(t.contents) |
| 37 | + pluralmatch = plural_re.match(t.contents) |
| 38 | + if endbmatch: |
| 39 | + if inplural: |
| 40 | + out.write(' {{ ngettext(%r,%r,count) }} ' % (''.join(singular), ''.join(plural))) |
| 41 | + for part in singular: |
| 42 | + out.write(blankout(part, 'S')) |
| 43 | + for part in plural: |
| 44 | + out.write(blankout(part, 'P')) |
| 45 | + else: |
| 46 | + out.write(' {{ gettext(%r) }} ' % ''.join(singular)) |
| 47 | + for part in singular: |
| 48 | + out.write(blankout(part, 'S')) |
| 49 | + intrans = False |
| 50 | + inplural = False |
| 51 | + singular = [] |
| 52 | + plural = [] |
| 53 | + elif pluralmatch: |
| 54 | + inplural = True |
| 55 | + else: |
| 56 | + raise SyntaxError("Translation blocks must not include other block tags: %s" % t.contents) |
| 57 | + elif t.token_type == TOKEN_VAR: |
| 58 | + if inplural: |
| 59 | + plural.append('%%(%s)s' % t.contents) |
| 60 | + else: |
| 61 | + singular.append('%%(%s)s' % t.contents) |
| 62 | + elif t.token_type == TOKEN_TEXT: |
| 63 | + if inplural: |
| 64 | + plural.append(t.contents) |
| 65 | + else: |
| 66 | + singular.append(t.contents) |
| 67 | + else: |
| 68 | + if t.token_type == TOKEN_BLOCK: |
| 69 | + imatch = inline_re.match(t.contents) |
| 70 | + bmatch = block_re.match(t.contents) |
| 71 | + cmatches = constant_re.findall(t.contents) |
| 72 | + if imatch: |
| 73 | + g = imatch.group(1) |
| 74 | + if g[0] == '"': g = g.strip('"') |
| 75 | + elif g[0] == "'": g = g.strip("'") |
| 76 | + out.write(' {{ gettext(%r) }} ' % g) |
| 77 | + elif bmatch: |
| 78 | + for fmatch in constant_re.findall(t.contents): |
| 79 | + out.write(' {{ _(%s) }} ' % fmatch) |
| 80 | + intrans = True |
| 81 | + inplural = False |
| 82 | + singular = [] |
| 83 | + plural = [] |
| 84 | + elif cmatches: |
| 85 | + for cmatch in cmatches: |
| 86 | + out.write(' {{ _(%s) }} ' % cmatch) |
| 87 | + else: |
| 88 | + out.write(blankout(t.contents, 'B')) |
| 89 | + elif t.token_type == TOKEN_VAR: |
| 90 | + parts = t.contents.split('|') |
| 91 | + cmatch = constant_re.match(parts[0]) |
| 92 | + if cmatch: |
| 93 | + out.write(' {{ _(%s) }} ' % cmatch.group(1)) |
| 94 | + for p in parts[1:]: |
| 95 | + if p.find(':_(') >= 0: |
| 96 | + out.write(' %s ' % p.split(':',1)[1]) |
| 97 | + else: |
| 98 | + out.write(blankout(p, 'F')) |
| 99 | + else: |
| 100 | + out.write(blankout(t.contents, 'X')) |
| 101 | + return out.getvalue() |
0 commit comments