Turn a Django template into something that is understood by xgettext. It does so by translating the Django translation tags into standard gettext function invocations.
(src, origin=None)
| 37 | |
| 38 | |
| 39 | def templatize(src, origin=None): |
| 40 | """ |
| 41 | Turn a Django template into something that is understood by xgettext. It |
| 42 | does so by translating the Django translation tags into standard gettext |
| 43 | function invocations. |
| 44 | """ |
| 45 | out = StringIO("") |
| 46 | message_context = None |
| 47 | intrans = False |
| 48 | inplural = False |
| 49 | trimmed = False |
| 50 | singular = [] |
| 51 | plural = [] |
| 52 | incomment = False |
| 53 | comment = [] |
| 54 | lineno_comment_map = {} |
| 55 | comment_lineno_cache = None |
| 56 | # Adding the u prefix allows gettext to recognize the string (#26093). |
| 57 | raw_prefix = "u" |
| 58 | |
| 59 | def join_tokens(tokens, trim=False): |
| 60 | message = "".join(tokens) |
| 61 | if trim: |
| 62 | message = trim_whitespace(message) |
| 63 | return message |
| 64 | |
| 65 | for t in Lexer(src).tokenize(): |
| 66 | if incomment: |
| 67 | if t.token_type == TokenType.BLOCK and t.contents == "endcomment": |
| 68 | content = "".join(comment) |
| 69 | translators_comment_start = None |
| 70 | for lineno, line in enumerate(content.splitlines(True)): |
| 71 | if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK): |
| 72 | translators_comment_start = lineno |
| 73 | for lineno, line in enumerate(content.splitlines(True)): |
| 74 | if ( |
| 75 | translators_comment_start is not None |
| 76 | and lineno >= translators_comment_start |
| 77 | ): |
| 78 | out.write(" # %s" % line) |
| 79 | else: |
| 80 | out.write(" #\n") |
| 81 | incomment = False |
| 82 | comment = [] |
| 83 | else: |
| 84 | comment.append(t.contents) |
| 85 | elif intrans: |
| 86 | if t.token_type == TokenType.BLOCK: |
| 87 | endbmatch = endblock_re.match(t.contents) |
| 88 | pluralmatch = plural_re.match(t.contents) |
| 89 | if endbmatch: |
| 90 | if inplural: |
| 91 | if message_context: |
| 92 | out.write( |
| 93 | " npgettext({p}{!r}, {p}{!r}, {p}{!r},count) ".format( |
| 94 | message_context, |
| 95 | join_tokens(singular, trimmed), |
| 96 | join_tokens(plural, trimmed), |
nothing calls this directly
no test coverage detected