-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
makemessages.py
281 lines (237 loc) · 10.3 KB
/
makemessages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
See https://zulip.readthedocs.io/en/latest/translating/internationalization.html
for background.
The contents of this file are taken from
https://github.com/niwinz/django-jinja/blob/master/django_jinja/management/commands/makemessages.py
Jinja2's i18n functionality is not exactly the same as Django's.
In particular, the tags names and their syntax are different:
1. The Django ``trans`` tag is replaced by a _() global.
2. The Django ``blocktrans`` tag is called ``trans``.
(1) isn't an issue, since the whole ``makemessages`` process is based on
converting the template tags to ``_()`` calls. However, (2) means that
those Jinja2 ``trans`` tags will not be picked up by Django's
``makemessages`` command.
There aren't any nice solutions here. While Jinja2's i18n extension does
come with extraction capabilities built in, the code behind ``makemessages``
unfortunately isn't extensible, so we can:
* Duplicate the command + code behind it.
* Offer a separate command for Jinja2 extraction.
* Try to get Django to offer hooks into makemessages().
* Monkey-patch.
We are currently doing that last thing. It turns out there we are lucky
for once: It's simply a matter of extending two regular expressions.
Credit for the approach goes to:
https://stackoverflow.com/questions/2090717
"""
import glob
import itertools
import json
import os
import re
import subprocess
from typing import Any, Collection, Dict, Iterator, List, Mapping
from django.core.management.base import CommandParser
from django.core.management.commands import makemessages
from django.template.base import BLOCK_TAG_END, BLOCK_TAG_START
from django.utils.translation import template
strip_whitespace_right = re.compile(
f"({BLOCK_TAG_START}-?\\s*(trans|pluralize).*?-{BLOCK_TAG_END})\\s+"
)
strip_whitespace_left = re.compile(
f"\\s+({BLOCK_TAG_START}-\\s*(endtrans|pluralize).*?-?{BLOCK_TAG_END})"
)
regexes = [
r"{{#tr}}([\s\S]*?)(?:{{/tr}}|{{#\*inline )", # '.' doesn't match '\n' by default
r'{{\s*t "([\s\S]*?)"\W*}}',
r"{{\s*t '([\s\S]*?)'\W*}}",
r'\(t "([\s\S]*?)"\)',
r'=\(t "([\s\S]*?)"\)(?=[^{]*}})',
r"=\(t '([\s\S]*?)'\)(?=[^{]*}})",
]
tags = [
("err_", "error"),
]
frontend_compiled_regexes = [re.compile(regex) for regex in regexes]
multiline_js_comment = re.compile(r"/\*.*?\*/", re.DOTALL)
singleline_js_comment = re.compile("//.*?\n")
def strip_whitespaces(src: str) -> str:
src = strip_whitespace_left.sub("\\1", src)
src = strip_whitespace_right.sub("\\1", src)
return src
class Command(makemessages.Command):
xgettext_options = makemessages.Command.xgettext_options
for func, tag in tags:
xgettext_options += [f'--keyword={func}:1,"{tag}"']
def add_arguments(self, parser: CommandParser) -> None:
super().add_arguments(parser)
parser.add_argument(
"--frontend-source",
default="web/templates",
help="Name of the Handlebars template directory",
)
parser.add_argument(
"--frontend-output",
default="locale",
help="Name of the frontend messages output directory",
)
parser.add_argument(
"--frontend-namespace",
default="translations.json",
help="Namespace of the frontend locale file",
)
def handle(self, *args: Any, **options: Any) -> None:
self.handle_django_locales(*args, **options)
self.handle_frontend_locales(**options)
def handle_frontend_locales(
self,
*,
frontend_source: str,
frontend_output: str,
frontend_namespace: str,
locale: List[str],
exclude: List[str],
all: bool,
**options: Any,
) -> None:
self.frontend_source = frontend_source
self.frontend_output = frontend_output
self.frontend_namespace = frontend_namespace
self.frontend_locale = locale
self.frontend_exclude = exclude
self.frontend_all = all
translation_strings = self.get_translation_strings()
self.write_translation_strings(translation_strings)
def handle_django_locales(self, *args: Any, **options: Any) -> None:
old_endblock_re = template.endblock_re
old_block_re = template.block_re
old_constant_re = template.constant_re
old_templatize = template.templatize
# Extend the regular expressions that are used to detect
# translation blocks with an "OR jinja-syntax" clause.
template.endblock_re = re.compile(
template.endblock_re.pattern + "|" + r"""^-?\s*endtrans\s*-?$"""
)
template.block_re = re.compile(
template.block_re.pattern + "|" + r"""^-?\s*trans(?:\s+(?!'|")(?=.*?=.*?)|\s*-?$)"""
)
template.plural_re = re.compile(
template.plural_re.pattern + "|" + r"""^-?\s*pluralize(?:\s+.+|-?$)"""
)
template.constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?')).*\)""")
def my_templatize(src: str, *args: Any, **kwargs: Any) -> str:
new_src = strip_whitespaces(src)
return old_templatize(new_src, *args, **kwargs)
template.templatize = my_templatize
try:
ignore_patterns = options.get("ignore_patterns", [])
ignore_patterns.append("docs/*")
ignore_patterns.append("templates/zerver/emails/custom/*")
ignore_patterns.append("var/*")
options["ignore_patterns"] = ignore_patterns
super().handle(*args, **options)
finally:
template.endblock_re = old_endblock_re
template.block_re = old_block_re
template.templatize = old_templatize
template.constant_re = old_constant_re
def extract_strings(self, data: str) -> List[str]:
translation_strings: List[str] = []
for regex in frontend_compiled_regexes:
for match in regex.findall(data):
match = match.strip()
match = " ".join(line.strip() for line in match.splitlines())
translation_strings.append(match)
return translation_strings
def ignore_javascript_comments(self, data: str) -> str:
# Removes multi line comments.
data = multiline_js_comment.sub("", data)
# Removes single line (//) comments.
data = singleline_js_comment.sub("", data)
return data
def get_translation_strings(self) -> List[str]:
translation_strings: List[str] = []
dirname = self.get_template_dir()
for dirpath, dirnames, filenames in os.walk(dirname):
for filename in [f for f in filenames if f.endswith(".hbs")]:
if filename.startswith("."):
continue
with open(os.path.join(dirpath, filename)) as reader:
data = reader.read()
translation_strings.extend(self.extract_strings(data))
for dirpath, dirnames, filenames in itertools.chain(
os.walk("web/src"), os.walk("web/shared/src")
):
for filename in [f for f in filenames if f.endswith((".js", ".ts"))]:
if filename.startswith("."):
continue
with open(os.path.join(dirpath, filename)) as reader:
data = reader.read()
data = self.ignore_javascript_comments(data)
translation_strings.extend(self.extract_strings(data))
extracted = subprocess.check_output(
[
"node_modules/.bin/formatjs",
"extract",
"--additional-function-names=$t,$t_html",
"--format=simple",
"--ignore=**/*.d.ts",
"web/src/**/*.js",
"web/src/**/*.ts",
]
)
translation_strings.extend(json.loads(extracted).values())
return list(set(translation_strings))
def get_template_dir(self) -> str:
return self.frontend_source
def get_namespace(self) -> str:
return self.frontend_namespace
def get_locales(self) -> Collection[str]:
locale = self.frontend_locale
exclude = self.frontend_exclude
process_all = self.frontend_all
# After calling super().handle(), default_locale_path gets set on self
# so that we can reuse it here.
default_locale_path = self.default_locale_path # type: ignore[attr-defined] # not in stubs
paths = glob.glob(f"{default_locale_path}/*")
all_locales = [os.path.basename(path) for path in paths if os.path.isdir(path)]
# Account for excluded locales
if process_all:
return all_locales
else:
locales = locale or all_locales
return set(locales) - set(exclude)
def get_base_path(self) -> str:
return self.frontend_output
def get_output_paths(self) -> Iterator[str]:
base_path = self.get_base_path()
locales = self.get_locales()
for path in [os.path.join(base_path, locale) for locale in locales]:
if not os.path.exists(path):
os.makedirs(path)
yield os.path.join(path, self.get_namespace())
def get_new_strings(
self, old_strings: Mapping[str, str], translation_strings: List[str], locale: str
) -> Dict[str, str]:
"""
Missing strings are removed, new strings are added and already
translated strings are not touched.
"""
new_strings = {} # Dict[str, str]
for k in translation_strings:
if locale == "en":
# For English language, translation is equal to the key.
new_strings[k] = old_strings.get(k, k)
else:
new_strings[k] = old_strings.get(k, "")
return new_strings
def write_translation_strings(self, translation_strings: List[str]) -> None:
for locale, output_path in zip(self.get_locales(), self.get_output_paths()):
self.stdout.write(f"[frontend] processing locale {locale}")
try:
with open(output_path) as reader:
old_strings = json.load(reader)
except (OSError, ValueError):
old_strings = {}
new_strings = self.get_new_strings(old_strings, translation_strings, locale)
with open(output_path, "w") as writer:
json.dump(new_strings, writer, indent=2, sort_keys=True)