-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyTranslate.py
156 lines (138 loc) · 3.54 KB
/
easyTranslate.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,iso639,json,os
version="0.1"
def inp(txt):
try:
if sys.version_info[0]<3:
return raw_input(txt)
else:
return input(txt)
except KeyboardInterrupt:
print("\nInterrupted by user.")
exit(1)
except Exception as e:
print("\n{}".format(e))
exit(2)
def writeTo(item,frm,addr):
if len(addr)>0:
frm[addr[0]]=writeTo(item,frm[addr[0]],addr[1:])
else:
frm=item
return frm
def translateJson(slang,sllang):
with open("langs/eng.json","r") as f:
engj=json.loads(f.read())
f.close()
newj=engj
# Getting strings and lists wich have to be translated
if sys.version_info[0]<3:
validTypes=[str,unicode,list]
else:
validTypes=[str,list]
addrs=[]
for x in engj: # this ↓ must be changed with a recursive loop
if type(engj[x]) in validTypes:
addrs.append([x])
else:
for y in engj[x]:
if type(engj[x][y]) in validTypes:
addrs.append([x,y])
else:
for z in engj[x][y]:
if type(engj[x][y][z]) in validTypes:
addrs.append([x,y,z])
else:
for k in engj[x][y][z]:
if type(engj[x][y][z][k]) in validTypes:
addrs.append([x,y,z,k])
print(k)
# Getting translations
for i,addr in enumerate(addrs):
engv=engj
for j in addr:
engv=engv[j]
if type(engv)==list:
engs=engv[0]
else:
engs=engv
print("--------")
print("{}/{}".format(i+1,len(addrs)))
print("> "+" > ".join(addr))
print("English: {}".format(repr(engs)))
# Todo: add multiple entry feature
newi=inp("{}: ".format(sllang))
if type(engv)==list:
newi=[newi]
newj=writeTo(newi,newj,addr)
# Save to file
outname="langs/{}.json".format(slang)
k=0
while os.path.isfile(outname):
outname="langs/{}-{}.json".format(slang,k)
k+=1
with open(outname,"w+") as out:
out.write(json.dumps(newj,indent=4))
out.close()
print("Writed to {}.".format(repr(outname)))
return 0
def translateBrain(slang,sllang):
with open("brains/eng.brain","r") as f:
engb=f.read().split("\n")
f.close()
newb=[]
for i,line in enumerate(engb):
if line.startswith("#") or line=="":
newb.append(line)
else:
print("--------")
print("{}/{}".format(i+1,len(engb)))
print("English: {}".format(line))
newb.append(inp("{}: ".format(sllang)))
# Save to file
outname="brains/{}.brain".format(slang)
k=0
while os.path.isfile(outname):
outname="brains/{}-{}.brain".format(slang,k)
k+=1
with open(outname,"w+") as out:
out.write("\n".join(newb))
out.close()
print("Writed to {}.".format(repr(outname)))
return 0
if __name__=="__main__":
langs=iso639.all()
e=False
while not e:
slang=inp("Select language (2 or 3 char lang code or name in english): ")
if not slang in langs:
print("Language not found. Try again.")
else:
if iso639.get_iso2(slang)=="eng":
print("You can't translate into english, it's already english.")
else:
e=True
slang=iso639.get_iso2(slang)
sllang=iso639.get_eng(slang)
translateOptions=["brain","json"]
e=False
while not e:
print("What you want to translate?")
for i,option in enumerate(translateOptions):
print("{} - {}".format(i,option.capitalize()))
whatToTranslate=inp("> ")
if whatToTranslate=="":
whatToTranslate=-1
else:
whatToTranslate=int(whatToTranslate)
if 0<=whatToTranslate<len(translateOptions):
e=True
else:
print("Option not valid. Try again.")
if translateOptions[whatToTranslate]=="json":
exit(translateJson(slang,sllang))
elif translateOptions[whatToTranslate]=="brain":
exit(translateBrain(slang,sllang))
else:
print("Unexpected error.")
exit(3)