-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmanage_translation.py
executable file
·217 lines (180 loc) · 7.69 KB
/
manage_translation.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
#!/usr/bin/env python
#
# This python file contains utility scripts to manage Python docs Polish translation.
# It has to be run inside the python-docs-pl git root directory.
#
# Inspired by django-docs-translations script by claudep.
#
# The following commands are available:
#
# * fetch: fetch translations from transifex.com and strip source lines from the
# files.
# * recreate_readme: recreate readme to update translation progress.
# * regenerate_tx_config: recreate configuration for all resources.
from argparse import ArgumentParser
from collections import Counter
import os
from re import match
from subprocess import call, run
import sys
LANGUAGE = 'pl'
def fetch():
"""
Fetch translations from Transifex, remove source lines.
"""
if call("tx --version", shell=True) != 0:
sys.stderr.write(
"The Transifex client app is required (pip install transifex-client).\n"
)
exit(1)
lang = LANGUAGE
pull_returncode = call(
f'tx pull -l {lang} --minimum-perc=1 --force --skip', shell=True
)
if pull_returncode != 0:
exit(pull_returncode)
for root, _, po_files in os.walk('.'):
for po_file in po_files:
if not po_file.endswith(".po"):
continue
po_path = os.path.join(root, po_file)
call(f'msgcat --no-location -o {po_path} {po_path}', shell=True)
RESOURCE_NAME_MAP = {'glossary_': 'glossary'}
PROJECT_SLUG = 'python-newest'
def recreate_tx_config():
"""
Regenerate Transifex client config for all resources.
"""
resources = _get_resources()
with open('.tx/config', 'w') as config:
config.writelines(
(
'[main]\n',
'host = https://www.transifex.com\n',
)
)
for resource in resources:
slug = resource['slug']
name = RESOURCE_NAME_MAP.get(slug, slug)
if slug == '0':
continue
elif '--' in slug:
directory, file_name = name.split('--')
if match(r'\d+_\d+', file_name):
file_name = file_name.replace('_', '.')
config.writelines(
(
'\n',
f'[{PROJECT_SLUG}.{slug}]\n',
f'trans.{LANGUAGE} = {directory}/{file_name}.po\n',
'type = PO\n',
'source_lang = en\n',
)
)
else:
config.writelines(
(
'\n',
f'[{PROJECT_SLUG}.{slug}]\n',
f'trans.{LANGUAGE} = {name}.po\n',
'type = PO\n',
'source_lang = en\n',
)
)
def _get_resources():
from requests import get
resources = []
offset = 0
if os.path.exists('.tx/api-key'):
with open('.tx/api-key') as f:
transifex_api_key = f.read()
else:
transifex_api_key = os.getenv('TX_TOKEN')
while True:
response = get(
f'https://api.transifex.com/organizations/python-doc/projects/{PROJECT_SLUG}/resources/',
params={'language_code': LANGUAGE, 'offset': offset},
auth=('api', transifex_api_key),
)
response.raise_for_status()
response_list = response.json()
resources.extend(response_list)
if len(response_list) < 100:
break
offset += len(response_list)
return resources
def _get_number_of_translators():
process = run(
['grep', '-ohP', r'(?<=^# )(.+)(?=, \d+$)', '-r', '.'],
capture_output=True,
text=True,
)
translators = [match('(.*) <.*>', t).group(1) for t in process.stdout.splitlines()]
unique_translators = Counter(translators).keys()
return len(unique_translators)
def recreate_readme():
def language_switcher(entry):
return (
entry['name'].startswith('bugs')
or entry['name'].startswith('tutorial')
or entry['name'].startswith('library--functions')
)
def average(averages, weights):
return sum([a * w for a, w in zip(averages, weights)]) / sum(weights)
resources = _get_resources()
filtered = list(filter(language_switcher, resources))
average_list = [e['stats']['translated']['percentage'] for e in filtered]
weights_list = [e['wordcount'] for e in filtered]
language_switcher_status = average(average_list, weights=weights_list) * 100
number_of_translators = _get_number_of_translators()
with open('README.md', 'w') as file:
file.write(
f'''\
Polskie tłumaczenie dokumentacji Pythona
========================================




Jeśli znalazłeś(-aś) błąd lub masz sugestię,
[dodaj zgłoszenie](https://github.com/python/python-docs-pl/issues) w tym projekcie lub
napraw go sam(a):
* Zarejestruj się na platformie [Transifex](https://www.transifex.com/) i wejdź na stronę
projektu [dokumentacji Pythona](https://www.transifex.com/python-doc/python-newest/).
* Na stronie projektu wybierz język polski.
* Naciśnij przycisk „Join this Team”, aby dołączyć do zespołu.
* Po dołączeniu do zespołu, wybierz zasób, który chcesz poprawić/zaktualizować.
Więcej informacji o używaniu Transifeksa znajdziesz w
[dokumentacji Transifeksa](https://docs.transifex.com/getting-started-1/translators).
**Postęp tłumaczenia**

Język polski pojawi się w przełączniku języków na docs.python.org,
[kiedy w pełni przetłumaczone będą](https://www.python.org/dev/peps/pep-0545/#add-translation-to-the-language-switcher):
* `bugs`,
* wszystkie zasoby z katalogu `tutorial`,
* `library/functions`.
**Jak obejrzeć najnowszy build dokumentacji przed opublikowaniem?**
Pobierz ostatnią zbudowaną dokumentację z listy artefaktów w ostatniej GitHub Action (zakładka Actions).
**Kanały komunikacji**
* [Python translations working group](https://mail.python.org/mailman3/lists/translation.python.org/)
* [Python Documentation Special Interest Group](https://www.python.org/community/sigs/current/doc-sig/)
**Licencja**
Zapraszając do współtworzenia projektu na platformie Transifex, proponujemy umowę na
przekazanie twoich tłumaczeń Python Software Foundation
[na licencji CC0](https://creativecommons.org/publicdomain/zero/1.0/deed.pl).
W zamian będzie widoczne, że jesteś tłumaczem(-ką) części, którą przetłumaczyłeś(-łaś).
Wyrażasz akceptację tej umowy przesyłając swoją pracę do włączenia do dokumentacji.
**Aktualizacja tłumaczeń**
* `./manage_translation.py recreate_tx_config`
* `./manage_translation.py fetch`
* `./manage_translation.py recreate_readme`
**Potencjalnie przydatne materiały**
* [polskie tłumaczenie dokumentacji Pythona 2.3](https://pl.python.org/docs/).
'''
)
if __name__ == "__main__":
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'recreate_readme')
parser = ArgumentParser()
parser.add_argument('cmd', nargs=1, choices=RUNNABLE_SCRIPTS)
options = parser.parse_args()
eval(options.cmd[0])()