| 1 |
import warnings |
| 2 |
from io import StringIO |
| 3 |
|
| 4 |
from django.template.base import Lexer, TokenType |
| 5 |
from django.utils.regex_helper import _lazy_re_compile |
| 6 |
|
| 7 |
from . import TranslatorCommentWarning, trim_whitespace |
| 8 |
|
| 9 |
TRANSLATOR_COMMENT_MARK = "Translators" |
| 10 |
|
| 11 |
dot_re = _lazy_re_compile(r"\S") |
| 12 |
|
| 13 |
|
| 14 |
def blankout(src, char): |
| 15 |
""" |
| 16 |
Change every non-whitespace character to the given char. |
| 17 |
Used in the templatize function. |
| 18 |
""" |
| 19 |
return dot_re.sub(char, src) |
| 20 |
|
| 21 |
|
| 22 |
context_re = _lazy_re_compile(r"""^\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?'))\s*""") |
| 23 |
inline_re = _lazy_re_compile( |
| 24 |
# Match the trans/translate 'some text' part. |
| 25 |
r"""^\s*trans(?:late)?\s+((?:"[^"]*?")|(?:'[^']*?'))""" |
| 26 |
# Match and ignore optional filters |
| 27 |
r"""(?:\s*\|\s*[^\s:]+(?::(?:[^\s'":]+|(?:"[^"]*?")|(?:'[^']*?')))?)*""" |
| 28 |
# Match the optional context part |
| 29 |
r"""(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?\s*""" |
| 30 |
) |
| 31 |
block_re = _lazy_re_compile( |
| 32 |
r"""^\s*blocktrans(?:late)?(\s+.*context\s+((?:"[^"]*?")|(?:'[^']*?')))?(?:\s+|$)""" |
| 33 |
) |
| 34 |
endblock_re = _lazy_re_compile(r"""^\s*endblocktrans(?:late)?$""") |
| 35 |
plural_re = _lazy_re_compile(r"""^\s*plural$""") |
| 36 |
constant_re = _lazy_re_compile(r"""_\(((?:".*?")|(?:'.*?'))\)""") |
| 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), |
| 97 |
p=raw_prefix, |
| 98 |
) |
| 99 |
) |
| 100 |
else: |
| 101 |
out.write( |
| 102 |
" ngettext({p}{!r}, {p}{!r}, count) ".format( |
| 103 |
join_tokens(singular, trimmed), |
| 104 |
join_tokens(plural, trimmed), |
| 105 |
p=raw_prefix, |
| 106 |
) |
| 107 |
) |
| 108 |
for part in singular: |
| 109 |
out.write(blankout(part, "S")) |
| 110 |
for part in plural: |
| 111 |
out.write(blankout(part, "P")) |
| 112 |
else: |
| 113 |
if message_context: |
| 114 |
out.write( |
| 115 |
" pgettext({p}{!r}, {p}{!r}) ".format( |
| 116 |
message_context, |
| 117 |
join_tokens(singular, trimmed), |
| 118 |
p=raw_prefix, |
| 119 |
) |
| 120 |
) |
| 121 |
else: |
| 122 |
out.write( |
| 123 |
" gettext({p}{!r}) ".format( |
| 124 |
join_tokens(singular, trimmed), |
| 125 |
p=raw_prefix, |
| 126 |
) |
| 127 |
) |
| 128 |
for part in singular: |
| 129 |
out.write(blankout(part, "S")) |
| 130 |
message_context = None |
| 131 |
intrans = False |
| 132 |
inplural = False |
| 133 |
singular = [] |
| 134 |
plural = [] |
| 135 |
elif pluralmatch: |
| 136 |
inplural = True |
| 137 |
else: |
| 138 |
filemsg = "" |
| 139 |
if origin: |
| 140 |
filemsg = "file %s, " % origin |
| 141 |
raise SyntaxError( |
| 142 |
"Translation blocks must not include other block tags: " |
| 143 |
"%s (%sline %d)" % (t.contents, filemsg, t.lineno) |
| 144 |
) |
| 145 |
elif t.token_type == TokenType.VAR: |
| 146 |
if inplural: |
| 147 |
plural.append("%%(%s)s" % t.contents) |
| 148 |
else: |
| 149 |
singular.append("%%(%s)s" % t.contents) |
| 150 |
elif t.token_type == TokenType.TEXT: |
| 151 |
contents = t.contents.replace("%", "%%") |
| 152 |
if inplural: |
| 153 |
plural.append(contents) |
| 154 |
else: |
| 155 |
singular.append(contents) |
| 156 |
else: |
| 157 |
# Handle comment tokens (`{# ... #}`) plus other constructs on |
| 158 |
# the same line: |
| 159 |
if comment_lineno_cache is not None: |
| 160 |
cur_lineno = t.lineno + t.contents.count("\n") |
| 161 |
if comment_lineno_cache == cur_lineno: |
| 162 |
if t.token_type != TokenType.COMMENT: |
| 163 |
for c in lineno_comment_map[comment_lineno_cache]: |
| 164 |
filemsg = "" |
| 165 |
if origin: |
| 166 |
filemsg = "file %s, " % origin |
| 167 |
warn_msg = ( |
| 168 |
"The translator-targeted comment '%s' " |
| 169 |
"(%sline %d) was ignored, because it wasn't " |
| 170 |
"the last item on the line." |
| 171 |
) % (c, filemsg, comment_lineno_cache) |
| 172 |
warnings.warn(warn_msg, TranslatorCommentWarning) |
| 173 |
lineno_comment_map[comment_lineno_cache] = [] |
| 174 |
else: |
| 175 |
out.write( |
| 176 |
"# %s" % " | ".join(lineno_comment_map[comment_lineno_cache]) |
| 177 |
) |
| 178 |
comment_lineno_cache = None |
| 179 |
|
| 180 |
if t.token_type == TokenType.BLOCK: |
| 181 |
imatch = inline_re.match(t.contents) |
| 182 |
bmatch = block_re.match(t.contents) |
| 183 |
cmatches = constant_re.findall(t.contents) |
| 184 |
if imatch: |
| 185 |
g = imatch[1] |
| 186 |
if g[0] == '"': |
| 187 |
g = g.strip('"') |
| 188 |
elif g[0] == "'": |
| 189 |
g = g.strip("'") |
| 190 |
g = g.replace("%", "%%") |
| 191 |
if imatch[2]: |
| 192 |
# A context is provided |
| 193 |
context_match = context_re.match(imatch[2]) |
| 194 |
message_context = context_match[1] |
| 195 |
if message_context[0] == '"': |
| 196 |
message_context = message_context.strip('"') |
| 197 |
elif message_context[0] == "'": |
| 198 |
message_context = message_context.strip("'") |
| 199 |
out.write( |
| 200 |
" pgettext({p}{!r}, {p}{!r}) ".format( |
| 201 |
message_context, g, p=raw_prefix |
| 202 |
) |
| 203 |
) |
| 204 |
message_context = None |
| 205 |
else: |
| 206 |
out.write(" gettext({p}{!r}) ".format(g, p=raw_prefix)) |
| 207 |
elif bmatch: |
| 208 |
for fmatch in constant_re.findall(t.contents): |
| 209 |
out.write(" _(%s) " % fmatch) |
| 210 |
if bmatch[1]: |
| 211 |
# A context is provided |
| 212 |
context_match = context_re.match(bmatch[1]) |
| 213 |
message_context = context_match[1] |
| 214 |
if message_context[0] == '"': |
| 215 |
message_context = message_context.strip('"') |
| 216 |
elif message_context[0] == "'": |
| 217 |
message_context = message_context.strip("'") |
| 218 |
intrans = True |
| 219 |
inplural = False |
| 220 |
trimmed = "trimmed" in t.split_contents() |
| 221 |
singular = [] |
| 222 |
plural = [] |
| 223 |
elif cmatches: |
| 224 |
for cmatch in cmatches: |
| 225 |
out.write(" _(%s) " % cmatch) |
| 226 |
elif t.contents == "comment": |
| 227 |
incomment = True |
| 228 |
else: |
| 229 |
out.write(blankout(t.contents, "B")) |
| 230 |
elif t.token_type == TokenType.VAR: |
| 231 |
parts = t.contents.split("|") |
| 232 |
cmatch = constant_re.match(parts[0]) |
| 233 |
if cmatch: |
| 234 |
out.write(" _(%s) " % cmatch[1]) |
| 235 |
for p in parts[1:]: |
| 236 |
if p.find(":_(") >= 0: |
| 237 |
out.write(" %s " % p.split(":", 1)[1]) |
| 238 |
else: |
| 239 |
out.write(blankout(p, "F")) |
| 240 |
elif t.token_type == TokenType.COMMENT: |
| 241 |
if t.contents.lstrip().startswith(TRANSLATOR_COMMENT_MARK): |
| 242 |
lineno_comment_map.setdefault(t.lineno, []).append(t.contents) |
| 243 |
comment_lineno_cache = t.lineno |
| 244 |
else: |
| 245 |
out.write(blankout(t.contents, "X")) |
| 246 |
return out.getvalue() |