MCPcopy
hub / github.com/django/django / templatize

Function templatize

django/utils/translation/template.py:39–246  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

37
38
39def 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),

Callers

nothing calls this directly

Calls 15

LexerClass · 0.90
join_tokensFunction · 0.85
blankoutFunction · 0.85
split_contentsMethod · 0.80
tokenizeMethod · 0.45
joinMethod · 0.45
writeMethod · 0.45
appendMethod · 0.45
matchMethod · 0.45
formatMethod · 0.45
countMethod · 0.45
splitMethod · 0.45

Tested by

no test coverage detected