-
Notifications
You must be signed in to change notification settings - Fork 1
/
locale.py
257 lines (198 loc) · 6.59 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
#!/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+') 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+') as json_file:
data = json.load(json_file)
if data[key]:
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+') 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+') 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 = [
'am',
'ar',
'bg',
'bn',
'ca',
'cs',
'da',
'de',
'el',
'en',
'es',
'et',
'fa',
'fi',
'fil',
'fr',
'gu',
'he',
'hi',
'hin',
'hr',
'hu',
'id',
'it',
'ja',
'kn',
'ko',
'lt',
'lv',
'ml',
'mr',
'ms',
'nb_NO',
'nl',
'no',
'pl',
'pt_BR',
'pt_PT',
'ro',
'ru',
'sk',
'sl',
'sr',
'sv',
'sw',
'ta',
'te',
'th',
'tr',
'uk',
'vi',
'zh_CN',
'zh_TW'
]
if os.path.exists('_locales/en/messages.json'):
file = open('_locales/en/messages.json', 'r+')
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+') 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()