-
Notifications
You must be signed in to change notification settings - Fork 5
/
importTranslation.py
executable file
·303 lines (283 loc) · 12.8 KB
/
importTranslation.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python3
import os
import re
import time
import json
translations = {}
cwd = os.getcwd()
regex = re.compile(r'{{@.+?}}')
select_language_page_locales_template = '''\
* <a href="javascript:forwardTo('{{@translation_country_key}}');">{{@selectLanguageName}}</a>
'''
config_js_locales_template = '''
'/{{@translation_country_key}}/': {
lang: '{{@translation_country_key}}',
title: '{{@heroText}}',
description: '{{@heroText}}'
},
'''
config_js_pwa_popup_template = '''
'/{{@translation_country_key}}/': {
message: "{{@newContentIsAvailable}}",
buttonText: "{{@refresh}}",
},
'''
config_js_search_template = '''
'/{{@translation_country_key}}/': {
placeholder: '{{@search}}',
},
'''
config_js_theme_config_template = '''
'/{{@translation_country_key}}/': {
editLinkText: '{{@editThisPage}}',
lastUpdatedText: '{{@lastUpdated}}',
selectLanguageText: '{{@language}}',
selectLanguageName: '{{@selectLanguageName}}',
navbar: [
{ text: '{{@homePage}}', link: '/{{@translation_country_key}}/' },
{ text: '{{@guidePage}}', link: '/{{@translation_country_key}}/guide/' },
{ text: '{{@download}}', link: '/{{@translation_country_key}}/download/' },
{ text: '{{@faqPage}}', link: '/{{@translation_country_key}}/faq/' },
{ text: '{{@API}}', link: '/{{@translation_country_key}}/api/' },
{ text: '{{@changelogPage}}', link: '/{{@translation_country_key}}/changelog/' },
{
text: '{{@more}}',
children: [
{
text: '{{@aboutPage}}',
children: [
{ text: '{{@contactUs}}', link: '/{{@translation_country_key}}/about/contactUs.md' },
{ text: '{{@specialThanks}}', link: '/{{@translation_country_key}}/thanks/' },
{ text: '{{@serverStatus}}', link: 'https://status.zidon.net' },
{ text: '{{@gitHubOrganization}}', link: 'https://github.com/FreezeYou/' },
],
},
{
text: '{{@linkPage}}',
children: [
{ text: '{{@autumnBox}}', link: 'https://atmb.top/?from=freezeyou' },
{ text: 'Zidon.NET', link: 'https://www.zidon.net' },
{ text: 'FreezeYou.NET', link: 'https://www.freezeyou.net' },
{ text: '{{@xn--f8qp88i.COM}}', link: 'https://www.xn--f8qp88i.com/' },
{ text: '{{@oldSite}}', link: 'https://freezeyou.playhi.net' },
],
},
],
},
],
sidebar: {
'/{{@translation_country_key}}/guide/': getGuideSidebar('{{@guidePage}}', '{{@changelog}}', '{{@faqPage}}', '{{@API}}'),
'/{{@translation_country_key}}/download/': getGuideSidebar('{{@guidePage}}', '{{@changelog}}', '{{@faqPage}}', '{{@API}}'),
'/{{@translation_country_key}}/changelog/': getGuideSidebar('{{@guidePage}}', '{{@changelog}}', '{{@faqPage}}', '{{@API}}'),
'/{{@translation_country_key}}/api/': getGuideSidebar('{{@guidePage}}', '{{@changelog}}', '{{@faqPage}}', '{{@API}}'),
'/{{@translation_country_key}}/faq/': getGuideSidebar('{{@guidePage}}', '{{@changelog}}', '{{@faqPage}}', '{{@API}}')
},
searchPlaceholder: '{{@search}}',
backToHome: '{{@backToHome}}',
notFound: [
`{{@notFound_1}}`,
`{{@notFound_2}}`,
`{{@notFound_3}}`,
`{{@notFound_4}}`
],
},
'''
def print_log(log, category="INFO", style="0;30"):
print("\033[{0}m{1} [{2}] {3}\033[0m".format(
style, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), category, log))
def load_translation_file():
path = cwd + "/translations"
file_list = os.listdir(path)
for tmp in file_list:
tmp_path = os.path.join(path, tmp)
if os.path.isfile(tmp_path) and tmp_path[tmp_path.rfind('.') + 1:].lower() == 'json':
with open(tmp_path, 'r') as f:
translations[tmp_path[tmp_path.rfind('/') + 1:tmp_path.rfind('.')]] = json.load(f)
print_log("Translation file loaded: " + tmp, "INFO", "0;36")
def pre_generate_documents_for_vuepress(path):
file_list = os.listdir(path)
for tmp in file_list:
tmp_path = os.path.join(path, tmp)
if os.path.isdir(tmp_path):
pre_generate_documents_for_vuepress(tmp_path)
elif tmp_path[tmp_path.rfind('.') + 1:].lower() == 'md':
with open(tmp_path, 'r') as f:
template_content = f.read()
for translation_country_key in translations.keys():
if translation_country_key == 'default':
continue
output_file_path = \
cwd + "/docs/" + translation_country_key + tmp_path[len(cwd) + 9:]
os.makedirs(output_file_path[:output_file_path.rfind("/")], 0o777, True)
generated_content = template_content
placeholders = regex.findall(generated_content)
for placeholder in placeholders:
key = placeholder[3:-2]
generated_content = generated_content.replace(
placeholder,
translations[translation_country_key].get(
key, translations['default'][key]
)
)
existed_content = None
if os.path.exists(output_file_path):
read_mode_output_file = open(output_file_path, "r")
existed_content = read_mode_output_file.read()
read_mode_output_file.close()
if generated_content == existed_content:
print_log(
"Document is up to date: " + output_file_path[len(cwd) + 1:],
"INFO", "0;36"
)
else:
with open(
output_file_path,
"w+"
) as output:
output.write(generated_content)
print_log(
"Document generated: " + output_file_path[len(cwd) + 1:],
"INFO", "0;36"
)
def pre_generate_config_js_and_select_lang_page_for_vuepress(
config_js_template_path, lang_page_template_path, lang_js_template_path
):
config_js_locales_generated = ""
config_js_pwa_popup_generated = ""
config_js_search_generated = ""
config_js_theme_config_generated = ""
select_language_page_locales_generated = ""
for translation_country_key in sorted(translations.keys()):
if translation_country_key == 'default':
continue
if translations[translation_country_key].get(
"selectLanguageName",
translations['default']["selectLanguageName"]
) == "In progress":
continue
generated_locales_content = config_js_locales_template.replace(
'{{@translation_country_key}}', translation_country_key)
placeholders = regex.findall(generated_locales_content)
for placeholder in placeholders:
key = placeholder[3:-2]
generated_locales_content = generated_locales_content.replace(
placeholder,
translations[translation_country_key].get(
key, translations['default'][key]
)
)
config_js_locales_generated += generated_locales_content
generated_pwa_popup_content = config_js_pwa_popup_template.replace(
'{{@translation_country_key}}', translation_country_key)
placeholders = regex.findall(generated_pwa_popup_content)
for placeholder in placeholders:
key = placeholder[3:-2]
generated_pwa_popup_content = generated_pwa_popup_content.replace(
placeholder,
translations[translation_country_key].get(
key, translations['default'][key]
)
)
config_js_pwa_popup_generated += generated_pwa_popup_content
generated_search_content = config_js_search_template.replace(
'{{@translation_country_key}}', translation_country_key)
placeholders = regex.findall(generated_search_content)
for placeholder in placeholders:
key = placeholder[3:-2]
generated_search_content = generated_search_content.replace(
placeholder,
translations[translation_country_key].get(
key, translations['default'][key]
)
)
config_js_search_generated += generated_search_content
generated_theme_config_content = config_js_theme_config_template.replace(
'{{@translation_country_key}}', translation_country_key)
placeholders = regex.findall(generated_theme_config_content)
for placeholder in placeholders:
key = placeholder[3:-2]
generated_theme_config_content = generated_theme_config_content.replace(
placeholder,
translations[translation_country_key].get(
key, translations['default'][key]
)
)
config_js_theme_config_generated += generated_theme_config_content
generated_lang_page_content = select_language_page_locales_template.replace(
'{{@translation_country_key}}', translation_country_key)
placeholders = regex.findall(generated_lang_page_content)
for placeholder in placeholders:
key = placeholder[3:-2]
generated_lang_page_content = generated_lang_page_content.replace(
placeholder,
translations[translation_country_key].get(
key, translations['default'][key]
)
)
select_language_page_locales_generated += generated_lang_page_content
with open(config_js_template_path, 'r') as input_file:
source = input_file.read()
with open(cwd + '/docs/.vuepress/config.js', 'w') as output_file:
output_file.write(
source.replace(
"/*{{@locales_content}}*/", config_js_locales_generated, 1
).replace(
"/*{{@pwa_popup_content}}*/", config_js_pwa_popup_generated, 1
).replace(
"/*{{@pwa_search_content}}*/", config_js_search_generated, 1
).replace(
"/*{{@pwa_theme_config_content}}*/",
config_js_theme_config_generated, 1
)
)
print_log("File config.js generated.", "INFO", "0;36")
with open(lang_page_template_path, 'r') as input_file:
source = input_file.read()
with open(cwd + '/docs/README.md', 'w') as output_file:
output_file.write(
source.replace(
"<!--{{@locales_generated_content}}-->",
select_language_page_locales_generated, 1
)
)
print_log(
"File SelectLanguagePage.markdown to README.md generated.",
"INFO", "0;36"
)
with open(lang_js_template_path, 'r') as input_file:
source = input_file.read()
available_langs = []
for translation_country_key in sorted(translations.keys()):
if translation_country_key == 'default':
continue
if translations[translation_country_key].get(
"selectLanguageName",
translations['default']["selectLanguageName"]
) != "In progress":
available_langs.append(translation_country_key)
with open(cwd + '/docs/.vuepress/public/assets/js/lang.min.js', 'w') as output_file:
output_file.write(
source.replace(
"/*{{@locales_availableLangs}}*/",
str(available_langs), 1
)
)
print_log(
"File lang.js and lang.min.js generated.",
"INFO", "0;36"
)
print_log("Current working directory: " + cwd, "INFO", "0;35")
print_log("Loading translation files...", "INFO", "0;34")
load_translation_file()
print_log("Generating translated documents for vuepress...", "INFO", "0;34")
pre_generate_documents_for_vuepress(cwd + "/template")
pre_generate_config_js_and_select_lang_page_for_vuepress(
cwd + "/template/config.js",
cwd + "/template/SelectLanguagePage.markdown",
cwd + "/template/lang.min.js"
)
print_log("Done.", "INFO", "0;32")
print_log(
"It's the time for using vuepress to generate the final documents!",
"INFO", "0;32"
)