-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_all_parametrized_messages.py
More file actions
298 lines (285 loc) · 14.1 KB
/
print_all_parametrized_messages.py
File metadata and controls
298 lines (285 loc) · 14.1 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import re
from collections import Counter, UserString
from sys import argv
import polib
import toml
import yaml
from rich.console import Console
from rich.table import Table
from rich.text import Text
django_clone_path = argv[2]
try:
language = argv[1]
except IndexError:
language = 'pl'
with open(f'{language}.toml') as rules_src:
rules = toml.load(rules_src)
auth = polib.pofile(f'{django_clone_path}/django/contrib/auth/locale/{language}/LC_MESSAGES/django.po')
user = auth.find('user').msgstr
users = auth.find('users').msgstr
group = auth.find('group').msgstr
groups = auth.find('groups').msgstr
permission = auth.find('permission').msgstr
permissions = auth.find('permissions').msgstr
sessions_file = polib.pofile(f'{django_clone_path}/django/contrib/sessions/locale/{language}/LC_MESSAGES/django.po')
session = sessions_file.find('session').msgstr
sessions = sessions_file.find('sessions').msgstr
sites_file = polib.pofile(f'{django_clone_path}/django/contrib/sites/locale/{language}/LC_MESSAGES/django.po')
site = sites_file.find('site').msgstr
sites = sites_file.find('sites').msgstr
redirects_file = polib.pofile(f'{django_clone_path}/django/contrib/redirects/locale/{language}/LC_MESSAGES/django.po')
redirect = redirects_file.find('redirect').msgstr
redirects = redirects_file.find('redirects').msgstr
flatpages_file = polib.pofile(f'{django_clone_path}/django/contrib/flatpages/locale/{language}/LC_MESSAGES/django.po')
flatpage = flatpages_file.find('flat page').msgstr
flatpages = flatpages_file.find('flat pages').msgstr
admin = polib.pofile(f'{django_clone_path}/django/contrib/admin/locale/{language}/LC_MESSAGES/django.po')
logentry = admin.find('log entry').msgstr
logentries = admin.find('log entries').msgstr
contenttypes_file = polib.pofile(
f'{django_clone_path}/django/contrib/contenttypes/locale/{language}/LC_MESSAGES/django.po'
)
contenttype = contenttypes_file.find('content type').msgstr
contenttypes = contenttypes_file.find('content types').msgstr
singulars = [
('user', user),
('group', group),
('permission', permission),
('session', session),
('site', site),
('redirect', redirect),
('flat page', flatpage),
('log entry', logentry),
('content type', contenttype),
]
plurals = [
('users', users),
('groups', groups),
('permissions', permissions),
('sessions', sessions),
('sites', sites),
('redirects', redirects),
('flat pages', flatpages),
('log entries', logentries),
('content types', contenttypes),
]
c: Counter = Counter()
console = Console()
def n_to_category(n):
if n == 1:
return 'one'
if 1 < n % 10 < 5 and (10 > n % 100 or n % 100 > 20):
return 'few'
return 'many'
def parse(rule: str, python_brace_format: bool, **translated_parameters) -> str:
prepared_parameters = {}
for param, value in translated_parameters.items():
if isinstance(value, tuple):
for_brace_param = UserString(value[1])
for_brace_param.accusative = rules.get(f'{value[0]}.accusative', value[1])
for_brace_param.genitive = rules.get(f'{value[0]}.genitive', value[1])
prepared_parameters |= {
param: for_brace_param,
f'{param}.accusative': rules.get(f'{value[0]}.accusative', value[1]),
f'{param}.genitive': rules.get(f'{value[0]}.genitive', value[1]),
}
else:
prepared_parameters |= {param: value}
if rule:
if isinstance(rule, str):
if not python_brace_format:
return rule % prepared_parameters
return rule.format(**prepared_parameters)
for element in rule:
if match := re.match('gender:(.*)', element):
flection_param = match.group(1)
key = translated_parameters[flection_param][0]
gendered = rule[element].get(rules.get(f'{key}.gender', 'other'), rule[element].get('other'))
if type(gendered) == str:
if not python_brace_format:
return gendered % prepared_parameters
return gendered.format(**prepared_parameters)
else:
return parse(gendered, python_brace_format, **translated_parameters)
if match := re.match('plurals:(.*)', element):
flection_param = match.group(1)
category = n_to_category(translated_parameters[flection_param])
pluralized = rule[element][category]
if type(pluralized) == str:
if not python_brace_format:
return pluralized % prepared_parameters
return pluralized.format(**prepared_parameters)
else:
return parse(pluralized, python_brace_format, **translated_parameters)
return ''
for package, domain in (
tuple(
(
(package, 'django')
for package in (
'contrib/admin',
# 'contrib/admindocs',
# 'contrib/auth',
# 'contrib/contenttypes',
# 'contrib/flatpages',
# 'contrib/gis',
# 'contrib/humanize',
# 'contrib/postgres',
# 'contrib/redirects',
# 'contrib/sessions',
# 'contrib/sites',
# 'conf',
)
)
)
# + (('contrib/admin', 'djangojs'),)
):
pofile = polib.pofile(f'{django_clone_path}/django/{package}/locale/{language}/LC_MESSAGES/{domain}.po')
pofile_en = polib.pofile(f'{django_clone_path}/django/{package}/locale/en/LC_MESSAGES/{domain}.po')
table = Table(show_lines=True, title=package)
table.add_column('current translation with examples')
table.add_column('enhanced translation')
table.add_column('enhanced examples')
for entry, entry_en in zip(pofile, pofile_en):
python_brace_format = 'python-brace-format' in entry.flags
if match_groups := re.findall(r'%(\(([a-z_]+)\))?s|{([a-z_]*)}', entry.msgid):
param_names = [match[1] or match[2] for match in match_groups]
c.update(param_names)
examples = []
shouldbeexamples = []
rule = rules.get(entry.msgid)
if param_names[0] == 'verbose_name_plural':
placeables = plurals
for key, translation in placeables:
examples.append(entry.msgstr % {'verbose_name_plural': translation})
for key, translation in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, verbose_name_plural=(key, translation)))
if param_names[0] == 'verbose_name':
placeables = singulars
for key, translation in placeables:
examples.append(entry.msgstr % {'verbose_name': translation})
for key, translation in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, verbose_name=(key, translation)))
if param_names[0] == 'items':
placeables = []
for singular, plural in zip(singulars, plurals):
placeables.append({'count': 1, 'items': singular})
placeables.append({'count': 2, 'items': plural})
placeables.append({'count': 5, 'items': plural})
for placeable in placeables:
examples.append(entry.msgstr % {'count': placeable['count'], 'items': placeable['items'][1]})
for placeable in placeables:
shouldbeexamples.append(
parse(rule, python_brace_format, count=placeable['count'], items=placeable['items'])
)
if param_names == ['name']:
placeables = []
for singular in singulars:
placeables.append({'name': singular, 'obj': 'maciek', 'key': 1})
for plural in plurals:
placeables.append({'name': plural, 'obj': 'maciek', 'key': 1})
for placeable in placeables:
# if 'python-format' in entry.flags:
examples.append(
entry.msgstr % {'obj': placeable['obj'], 'name': placeable['name'][1], 'key': placeable['key']}
)
# elif 'python-brace-format' in entry.flags:
for placeable in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, **placeable))
if param_names == ['name', 'object']:
placeables = [{'name': singular, 'object': 'maciek'} for singular in singulars]
for placeable in placeables:
examples.append(entry.msgstr.format(name=placeable['name'][1], object=placeable['object']))
for placeable in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, **placeable))
if param_names == ['fields', 'name', 'object']:
placeables = [{'name': singular, 'fields': 'username', 'object': 'maciek'} for singular in singulars]
for placeable in placeables:
examples.append(
entry.msgstr.format(
name=placeable['name'][1], fields=placeable['fields'], object=placeable['object']
)
)
for placeable in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, **placeable))
if param_names == ['name', 'obj']:
placeables = [{'name': singular, 'obj': 'maciek'} for singular in singulars]
for placeable in placeables:
if python_brace_format:
examples.append(entry.msgstr.format(name=placeable['name'][1], obj=placeable['obj']))
else:
examples.append(entry.msgstr % {'name': placeable['name'][1], 'obj': placeable['obj']})
for placeable in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, **placeable))
if param_names == ['name', 'obj', 'name']:
placeables = [{'name': singular, 'obj': 'maciek'} for singular in singulars]
for placeable in placeables:
examples.append(entry.msgstr.format(name=placeable['name'][1], obj=placeable['obj']))
for placeable in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, **placeable))
if param_names == ['name', 'key']:
placeables = [{'name': singular, 'key': 'maciek'} for singular in singulars]
for placeable in placeables:
if python_brace_format:
examples.append(entry.msgstr.format(name=placeable['name'][1], key=placeable['key']))
else:
examples.append(entry.msgstr % {'name': placeable['name'][1], 'key': placeable['key']})
for placeable in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, **placeable))
if param_names == ['']:
placeables = [{'': singular} for singular in singulars]
for placeable in placeables:
if python_brace_format:
examples.append(entry.msgstr.format(placeable[''][1]))
else:
examples.append(entry.msgstr % (placeable[''][1],))
for placeable in placeables:
shouldbeexamples.append(parse(rule, python_brace_format, **placeable))
if param_names == ['count', 'name']:
placeables = []
for singular, plural in zip(singulars, plurals):
placeables.append({'count': 1, 'name': singular})
placeables.append({'count': 2, 'name': plural})
placeables.append({'count': 5, 'name': plural})
for placeable in placeables:
examples.append(entry.msgstr % {'count': placeable['count'], 'name': placeable['name'][1]})
for placeable in placeables:
shouldbeexamples.append(
parse(rule, python_brace_format, count=placeable['count'], name=placeable['name'])
)
if param_names == ['class_name', 'instance', 'related_objects']:
placeables = []
for singular in singulars:
placeables.append({'class_name': singular, 'instance': 'maciek', 'related_objects': 'user maciek'})
for placeable in placeables:
examples.append(
entry.msgstr
% {
'class_name': placeable['class_name'][1],
'instance': placeable['instance'],
'related_objects': placeable['related_objects'],
}
)
for placeable in placeables:
shouldbeexamples.append(
parse(
rule,
python_brace_format,
class_name=placeable['class_name'],
instance=placeable['instance'],
related_objects=placeable['related_objects'],
)
)
shouldbeoutput = ''
if not rule:
shouldbeoutput += 'no need to enhance or not parametrized with models verbose name'
if shouldbeexamples:
shouldbeoutput = '\n'.join(shouldbeexamples)
current = Text()
current.append('\n'.join(f'{path}:{line}' for path, line in entry_en.occurrences) + '\n')
current.append(entry.msgid + '\n', "bold")
current.append(entry.msgstr + '\n', "bold")
current.append('\n'.join(examples))
table.add_row(current, yaml.dump(rule, allow_unicode=True), shouldbeoutput)
console.print(table)
console.print('names of placeholders with count of their occurrence:', str(c))