-
-
Notifications
You must be signed in to change notification settings - Fork 551
/
locale.py
338 lines (279 loc) · 7.04 KB
/
locale.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# >>> TABLE OF CONTENTS:
# ------------------------------------------------------------------------------
# 1.0 Import modules
# 2.0 Lower camel case
# 3.0 Get list of files
# 4.0 Add item
# 5.0 Remove item
# 6.0 Change key
# 7.0 Decode
# 8.0 Upgrade
# 9.0 Initialization
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# 1.0 IMPORT MODULES
# ------------------------------------------------------------------------------
import io
import json
import os
import pathlib
import re
import sys
# ------------------------------------------------------------------------------
# 2.0 LOWER CAMEL CASE
# ------------------------------------------------------------------------------
def lowerCamelCase(string):
string = re.sub(r"(-|_)+", ' ', string).title()
string = re.sub(r"[^a-zA-Z0-9]", '', string)
return string[0].lower() + string[1:]
# ------------------------------------------------------------------------------
# 3.0 GET LIST OF FILES
# ------------------------------------------------------------------------------
def getListOfFiles(path):
allFiles = list()
for entry in os.listdir(path):
fullPath = os.path.join(path, entry)
if not os.path.isdir(fullPath):
allFiles.append(fullPath)
for entry in os.listdir(path):
fullPath = os.path.join(path, entry)
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
return allFiles
# ------------------------------------------------------------------------------
# 4.0 ADD ITEM
# ------------------------------------------------------------------------------
def addItem(allFiles):
message = input('Enter your message: ')
camelized_message = lowerCamelCase(message)
for keyFile in allFiles:
with open(keyFile, 'r+', encoding='utf-8') as json_file:
data = json.load(json_file)
if (camelized_message in data) == False:
data[camelized_message] = {'message': message}
json_file.seek(0)
json.dump(data, json_file, ensure_ascii=False, indent=4, sort_keys=True)
json_file.truncate()
# ------------------------------------------------------------------------------
# 5.0 REMOVE ITEM
# ------------------------------------------------------------------------------
def removeItem(allFiles):
key = input('Enter your key (lowerCamelCase): ')
for keyFile in allFiles:
with open(keyFile, 'r+', encoding='utf-8') as json_file:
data = json.load(json_file)
if key in data:
del data[key]
json_file.seek(0)
json.dump(data, json_file, ensure_ascii=False, indent=4,
sort_keys=True)
json_file.truncate()
# ------------------------------------------------------------------------------
# 6.0 CHANGE KEY
# ------------------------------------------------------------------------------
def changeKey(allFiles):
old_key = input('Enter key: ')
new_key = input('Enter new key: ')
for keyFile in allFiles:
with open(keyFile, 'r+', encoding='utf-8') as file:
data = json.load(file)
if old_key in data:
data[new_key] = data[old_key]
del data[old_key]
file.seek(0)
json.dump(data, file, ensure_ascii=False, indent=4, sort_keys=True)
file.truncate()
# ------------------------------------------------------------------------------
# 7.0 DECODE
# ------------------------------------------------------------------------------
def decodeCharacters(allFiles):
for keyFile in allFiles:
with open(keyFile, 'r+', encoding='utf-8') as json_file:
data = json.load(json_file)
json_file.seek(0)
json.dump(data, json_file, ensure_ascii=False, indent=4,
sort_keys=True)
json_file.truncate()
# ------------------------------------------------------------------------------
# 8.0 UPGRADE
# ------------------------------------------------------------------------------
def upgrade():
locales = [
'af_ZA',
'am',
'ar',
'ar_BH',
'ar_EG',
'ar_SA',
'bg',
'bg_BG',
'bn',
'ca',
'ca_ES',
'ceb_PH',
'ch_GU',
'cs',
'cs_CZ',
'csb_PL',
'cv_CU',
'da',
'da_DK',
'de',
'de_CH',
'de_DE',
'el',
'el_CY',
'el_GR',
'en',
'en_GB',
'en_PT',
'en_US',
'eo_UY',
'es',
'es_419',
'es_ES',
'et',
'et_EE',
'fa',
'fa_IR',
'fi',
'fi_FI',
'fil',
'fil_PH',
'fj_FJ',
'fo_FO',
'fr',
'fr_BE',
'fr_CA',
'fr_CH',
'fr_FR',
'fr_LU',
'fr_QC',
'fra_DE',
'fy_NL',
'ga_IE',
'got_DE',
'gu',
'he',
'he_IL',
'hi',
'hi_IN',
'hil_PH',
'hr',
'hu',
'hu_HU',
'id',
'it',
'it_IT',
'ja',
'ja_JP',
'kab_KAB',
'kl_GL',
'kn',
'kn_IN',
'ko',
'ko_KR',
'ks_IN',
'ky_KG',
'lo_LA',
'lt',
'lv',
'mai_IN',
'me_ME',
'mg_MG',
'ml',
'ml_IN',
'mn_MN',
'moh_CA',
'mos_MOS',
'mr',
'ms',
'ms_BN',
'ms_MY',
'my_MM',
'na_NR',
'nb_NO',
'nds_DE',
'ne_IN',
'ne_NP',
'ng_NA',
'nl',
'nl_NL',
'no',
'no_NO',
'pam_PH',
'pcm_NG',
'pl',
'pl_PL',
'ps_AF',
'pt_BR',
'pt_PT',
'ro',
'ro_RO',
'ru',
'ru_RU',
'si',
'sk',
'sl',
'sr',
'sr_Cyrl_ME',
'sr_SP',
'sv',
'sv_SE',
'sw',
'ta',
'te',
'th',
'tr',
'tr_TR',
'uk',
'uk_UA',
'vi',
'vi_VN',
'vls_BE',
'zh_CN',
'zh_TW',
]
if os.path.exists('../_locales/en/messages.json'):
file = open('../_locales/en/messages.json', 'r+', encoding='utf-8')
default_locale = json.load(file)
file.close()
else:
default_locale = {}
for locale in locales:
path = '../_locales/' + locale
if not os.path.exists(path):
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
file = io.open(path + '/messages.json', mode='w', encoding='utf-8')
json.dump(default_locale, file, ensure_ascii=False, indent=4, sort_keys=True)
file.close()
else:
with open(path + '/messages.json', 'r+', encoding='utf-8') as file:
data = json.load(file)
file.seek(0)
for key in default_locale:
if (key in data) == False:
data[key] = default_locale[key]
json.dump(data, file, ensure_ascii=False, indent=4, sort_keys=True)
file.truncate()
file.close()
# ------------------------------------------------------------------------------
# 9.0 INITIALIZATION
# ------------------------------------------------------------------------------
if not os.path.exists('../_locales/'):
pathlib.Path('../_locales/').mkdir(parents=True, exist_ok=True)
allFiles = getListOfFiles('../_locales/')
for arg in sys.argv:
if arg == '-add':
addItem(allFiles)
elif arg == '-remove':
removeItem(allFiles)
elif arg == '-decode':
decodeCharacters(allFiles)
elif arg == '-change-key':
changeKey(allFiles)
elif arg == '-upgrade':
upgrade()