|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2007 Edgewall Software |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# This software is licensed as described in the file COPYING, which |
| 7 | +# you should have received as part of this distribution. The terms |
| 8 | +# are also available at http://babel.edgewall.org/wiki/License. |
| 9 | +# |
| 10 | +# This software consists of voluntary contributions made by many |
| 11 | +# individuals. For the exact contribution history, see the revision |
| 12 | +# history and logs, available at http://babel.edgewall.org/log/. |
| 13 | + |
| 14 | +from babel.core import * |
| 15 | + |
| 16 | +from django.conf import settings |
| 17 | +settings.configure(USE_I18N=True) |
| 18 | +from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK |
| 19 | +from django.utils.translation.trans_real import inline_re, block_re, \ |
| 20 | + endblock_re, plural_re, \ |
| 21 | + constant_re |
| 22 | + |
| 23 | +def extract_django(fileobj, keywords, comment_tags, options): |
| 24 | + """Extract messages from Django template files. |
| 25 | +
|
| 26 | + :param fileobj: the file-like object the messages should be extracted from |
| 27 | + :param keywords: a list of keywords (i.e. function names) that should |
| 28 | + be recognized as translation functions |
| 29 | + :param comment_tags: a list of translator tags to search for and |
| 30 | + include in the results |
| 31 | + :param options: a dictionary of additional options (optional) |
| 32 | + :return: an iterator over ``(lineno, funcname, message, comments)`` |
| 33 | + tuples |
| 34 | + :rtype: ``iterator`` |
| 35 | + """ |
| 36 | + intrans = False |
| 37 | + inplural = False |
| 38 | + singular = [] |
| 39 | + plural = [] |
| 40 | + lineno = 1 |
| 41 | + for t in Lexer(fileobj.read(), None).tokenize(): |
| 42 | + lineno += t.contents.count('\n') |
| 43 | + if intrans: |
| 44 | + if t.token_type == TOKEN_BLOCK: |
| 45 | + endbmatch = endblock_re.match(t.contents) |
| 46 | + pluralmatch = plural_re.match(t.contents) |
| 47 | + if endbmatch: |
| 48 | + if inplural: |
| 49 | + yield lineno, 'ngettext', (unicode(''.join(singular)), |
| 50 | + unicode(''.join(plural))), [] |
| 51 | + else: |
| 52 | + yield lineno, None, unicode(''.join(singular)), [] |
| 53 | + intrans = False |
| 54 | + inplural = False |
| 55 | + singular = [] |
| 56 | + plural = [] |
| 57 | + elif pluralmatch: |
| 58 | + inplural = True |
| 59 | + else: |
| 60 | + raise SyntaxError('Translation blocks must not include ' |
| 61 | + 'other block tags: %s' % t.contents) |
| 62 | + elif t.token_type == TOKEN_VAR: |
| 63 | + if inplural: |
| 64 | + plural.append('%%(%s)s' % t.contents) |
| 65 | + else: |
| 66 | + singular.append('%%(%s)s' % t.contents) |
| 67 | + elif t.token_type == TOKEN_TEXT: |
| 68 | + if inplural: |
| 69 | + plural.append(t.contents) |
| 70 | + else: |
| 71 | + singular.append(t.contents) |
| 72 | + else: |
| 73 | + if t.token_type == TOKEN_BLOCK: |
| 74 | + imatch = inline_re.match(t.contents) |
| 75 | + bmatch = block_re.match(t.contents) |
| 76 | + cmatches = constant_re.findall(t.contents) |
| 77 | + if imatch: |
| 78 | + g = imatch.group(1) |
| 79 | + if g[0] == '"': |
| 80 | + g = g.strip('"') |
| 81 | + elif g[0] == "'": |
| 82 | + g = g.strip("'") |
| 83 | + yield lineno, None, unicode(g), [] |
| 84 | + elif bmatch: |
| 85 | + intrans = True |
| 86 | + inplural = False |
| 87 | + singular = [] |
| 88 | + plural = [] |
| 89 | + elif cmatches: |
| 90 | + for cmatch in cmatches: |
| 91 | + yield lineno, None, unicode(cmatch), [] |
| 92 | + elif t.token_type == TOKEN_VAR: |
| 93 | + parts = t.contents.split('|') |
| 94 | + cmatch = constant_re.match(parts[0]) |
| 95 | + if cmatch: |
| 96 | + yield lineno, None, unicode(cmatch.group(1)), [] |
| 97 | + for p in parts[1:]: |
| 98 | + if p.find(':_(') >= 0: |
| 99 | + p1 = p.split(':',1)[1] |
| 100 | + if p1[0] == '_': |
| 101 | + p1 = p1[1:] |
| 102 | + if p1[0] == '(': |
| 103 | + p1 = p1.strip('()') |
| 104 | + if p1[0] == "'": |
| 105 | + p1 = p1.strip("'") |
| 106 | + elif p1[0] == '"': |
| 107 | + p1 = p1.strip('"') |
| 108 | + yield lineno, None, unicode(p1), [] |
0 commit comments