Add script for fixing translation
This commit is contained in:
parent
60c5217f3a
commit
0d4da70ba0
106
translation/convert_translation.py
Normal file
106
translation/convert_translation.py
Normal file
|
@ -0,0 +1,106 @@
|
|||
import os
|
||||
import io
|
||||
import shutil
|
||||
|
||||
def strSplit(str):
|
||||
result = []
|
||||
ind1 = str.find('"')
|
||||
ind2 = str.find('"', ind1+1)
|
||||
result.append(str[ind1+1:ind2])
|
||||
ind3 = str.find('"', ind2+1)
|
||||
indLast = str.rfind('"')
|
||||
result.append(str[ind3+1:indLast])
|
||||
return result
|
||||
app_names = ['documenteditor', 'presentationeditor', 'spreadsheeteditor']
|
||||
app_types = ['embed', 'main', 'mobile']
|
||||
prefix_apps = '../apps/'
|
||||
prefix_src = 'src/'
|
||||
prefix_dest = 'dest/'
|
||||
str_apps = []
|
||||
langs = []
|
||||
for i in range(len(app_names)):
|
||||
for j in range (len(app_types)):
|
||||
locale_path = prefix_apps + app_names[i] + '/' + app_types[j] + '/locale/'
|
||||
src_path = prefix_src + app_names[i] + '/' + app_types[j] + '/locale/'
|
||||
dest_path = prefix_dest + app_names[i] + '/' + app_types[j] + '/locale/'
|
||||
print(locale_path)
|
||||
if os.path.isdir(locale_path):
|
||||
langs = os.listdir(locale_path)
|
||||
if not os.path.isdir(dest_path):
|
||||
os.makedirs(dest_path)
|
||||
if len(langs)>0:
|
||||
try:
|
||||
f = io.open(locale_path + 'en.json', mode='r', encoding='utf-8')
|
||||
except Exception:
|
||||
continue
|
||||
lines = f.readlines()
|
||||
f.close()
|
||||
str_apps = []
|
||||
for k in range(1, len(lines)-1):
|
||||
arr = strSplit(lines[k])
|
||||
if len(arr) > 0:
|
||||
str_apps.append(arr[0])
|
||||
for l in range(len(langs)):
|
||||
lang = langs[l].split('.')[0]
|
||||
f = io.open(locale_path + lang+ '.json', mode='r', encoding='utf-8')
|
||||
lines = f.readlines()
|
||||
f.close()
|
||||
try:
|
||||
f = io.open(src_path + lang+ '.json', mode='r', encoding='utf-8')
|
||||
except Exception:
|
||||
f.close()
|
||||
f = io.open(dest_path + lang + '.json', mode='w', encoding='utf-8')
|
||||
f.writelines(lines)
|
||||
f.close()
|
||||
print(' ' + lang+ '.json - copied')
|
||||
continue
|
||||
d_locale = dict()
|
||||
d_src = dict()
|
||||
# locale
|
||||
for k in range(1, len(lines)-1):
|
||||
arr = strSplit(lines[k])
|
||||
if len(arr) > 1:
|
||||
d_locale[arr[0]] = arr[1]
|
||||
# src
|
||||
lines = f.readlines()
|
||||
for k in range(1, len(lines)-1):
|
||||
arr = strSplit(lines[k])
|
||||
if len(arr) > 1:
|
||||
d_src[arr[0]] = arr[1]
|
||||
f.close()
|
||||
f = io.open(dest_path + lang + '.json', mode='w', encoding='utf-8')
|
||||
f.write(u'{')
|
||||
f.write(u'\n')
|
||||
changed = 'not changed'
|
||||
isFirst = True
|
||||
arr_len = len(str_apps)
|
||||
for k in range(arr_len):
|
||||
str = str_apps[k]
|
||||
if str.find('del_')==0:
|
||||
continue
|
||||
if str in d_src:
|
||||
changed = 'changed'
|
||||
str = ' "' + str + '": "' + d_src[str] + '"'
|
||||
if isFirst:
|
||||
isFirst = False
|
||||
else:
|
||||
f.write(u',\n')
|
||||
f.write(str)
|
||||
elif str in d_locale:
|
||||
str = ' "' + str + '": "' + d_locale[str] + '"'
|
||||
if isFirst:
|
||||
isFirst = False
|
||||
else:
|
||||
f.write(u',\n')
|
||||
f.write(str)
|
||||
f.write(u'\n')
|
||||
f.write(u'}')
|
||||
f.close()
|
||||
print(' ' + lang + '.json - ' + changed)
|
||||
add_langs = os.listdir(src_path)
|
||||
for l in range(len(langs)):
|
||||
add_langs.remove(langs[l])
|
||||
for l in range(len(add_langs)):
|
||||
lang = add_langs[l]
|
||||
shutil.copyfile(src_path + lang, dest_path + lang)
|
||||
print(' ' + lang + ' - ' + 'added')
|
Loading…
Reference in a new issue