-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform.py
123 lines (87 loc) · 2.65 KB
/
Transform.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
from Tkinter import *
from tkFileDialog import *
import string
import json
import codecs
import io
global file
def callbackJson():
fileName = askopenfilename(parent=root, filetypes = (("json files", "*.js"), ("All files", "*.*")))
if fileName:
with codecs.open(fileName, encoding='utf-8', mode='r') as f:
read_data = f.read()
f.closed
parseJson(read_data)
def callbackExcel():
fileName = askopenfilename(parent=root, filetypes = (("Comma separated value", "*.csv"), ("All files", "*.*")))
if fileName:
with codecs.open(fileName, encoding='utf-8', mode='r') as f:
read_data = f.read()
f.closed
parseCsv(read_data)
def parseCsv(data):
lines = data.split('\n')
lines = filter(bool, lines)
for i in range(0, len(lines)):
lines[i] = lines[i].strip("\r")
print 'Found {0} lines'.format(len(lines) - 1)
delimiter = ';'
languages = lines[0].split(delimiter)
languages = filter(bool, languages)
print "Translating to", len(languages) - 1, "languages"
trans = {}
lin = 0
wrong = 1
for i in range(1, len(lines)):
words = lines[i].split(delimiter)
words = filter(bool, words)
dict = {}
if len(words) == len(languages):
lin = lin + 1
for j in range(1, len(languages)):
dict.update({languages[j]: words[j]})
trans[words[0]] = dict;
else:
print wrong, "Can't tanslate " + lines[i]
wrong = wrong + 1
textJson = json.dumps(trans, ensure_ascii=False)
textJson = textJson.replace("},", "},\n")
textJson = "translations = " + textJson
with io.open('labels.js', 'w', encoding='utf8') as json_file:
json_file.write(textJson)
json_file.close()
print lin, " records translated"
return 0
def parseJson(data):
if len(data) < 15:
return -1
#remove TRANSLATION string which mangle json format
data = data[15:]
dict = json.loads(data)
csv = "id"
itemKey, itemValue = dict.popitem()
#csv delimiter
delimiter = ";"
#create csv header
for lan, tran in itemValue.iteritems():
csv = csv + delimiter + lan
csv += '\n'
dict[itemKey] = itemValue
#create records
for id, translation in dict.iteritems():
csv = csv + id
for lan, langTrans in translation.iteritems():
csv = csv + delimiter + langTrans
csv += '\n'
print csv
text_file = codecs.open('out.csv', encoding='utf_8', mode='w+')
text_file.write(csv)
text_file.close()
return 0
root = Tk()
root.wm_title("COPQ translation converter")
w = Label(root, text="Please choose a file to convert.")
b1 = Button(text='Choose a Json file', state=DISABLED, command=callbackJson).pack(fill=X)
b2 = Button(text='Choose a Excel-csv file', command=callbackExcel).pack(fill=X)
w.pack()
root.mainloop()