-
Notifications
You must be signed in to change notification settings - Fork 7
/
translate.py
286 lines (234 loc) · 11 KB
/
translate.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
import os
import re
import json
import xmltodict
# default dirs
xml_dir = os.path.join("Xml", "tables", "master")
jp_xml_dir = os.path.join("jp", xml_dir)
en_xml_dir = os.path.join("en", xml_dir)
kc3_trans_dir = "kc3-vita-translations"
kc3_en_dir = os.path.join(kc3_trans_dir, "en")
kc3_jp_dir = os.path.join(kc3_trans_dir, "jp")
# maybe kc3 translation should be a submodule, choose language as a argument
def ships(lang='en'): # translate all ship names
# open xml
shipxml = xmltodict.parse(open(os.path.join(jp_xml_dir, 'mst_ship.xml'), 'rb'))
# get KC3 JSON, kanji name as key and translation as value
shiplist = json.load(open(os.path.join(kc3_trans_dir, lang, 'ships.json'), 'r'))
# replace all names with corresponding unicode string
for item in shipxml['mst_ship_data']['mst_ship']:
# actually, don't rename Nashi, game crashes if it can't find it
#if item['Name'] == 'なし': # None
#item['Name'] = 'None'
#print(item['Id'], item['Name'])
#continue
# 544 装甲空母鬼 = Armored Carrier Demon
if item['Name'] == "装甲空母鬼":
item['Name'] = 'Armored Carrier Demon'
print(item['Id'], item['Name'])
continue
# 545 装甲空母姫 = Armored Carrier Priness
if item['Name'] == "装甲空母姫":
item['Name'] = 'Armored Carrier Princess'
print(item['Id'], item['Name'])
continue
# 650 運河棲姫 = Canal Princess, unique to Vita
if item['Name'] == "運河棲姫":
item['Name'] = 'Canal Princess'
print(item['Id'], item['Name'])
continue
try:
# render Kai and Ni to romaji with space separation
# also render 甲 (corresponds to https://en.wikipedia.org/wiki/Celestial_stem ) as A... (in the future 乙: B, 丙: C, 丁: D)
item['Name'] = item['Name'].replace('改', ' Kai').replace('二', ' Ni').replace('甲', ' A')
# since event specifics only start from ID 901, only start checking if it is greater than this
if int(item['Id']) >= 901:
events = {"年末": "Year-end", "正月": "New_Year", "梅雨": "Rainy_Season", "夏": "Summer", "秋": "Autumn", "Valentine": "Valentine", "Xmas": "Xmas"} # underscore as scaffolding
for event in events.keys():
if item['Name'].find(event) != -1:
processed = item['Name'].replace(event, event + " ")
fullname = processed.split()
fullname[0] = events[event].replace("_", " ")
translation = shiplist[fullname[1]]
fullname[1] = translation
item["Name"] = " ".join(fullname)
break # name found, no more searching needed
else:
# split full name into components
fullname = item['Name'].split()
# take kanji name (without trailing words) and match to translation
translation = shiplist[fullname[0]]
if len(fullname) == 1: # just write base name
item['Name'] = translation
else: # add trailing kai and ni with spaces
fullname[0] = translation
item['Name'] = " ".join(fullname)
except KeyError: # if name doesn't exist, don't edit
pass
print(item['Id'], item['Name'])
# save changes to file
print("Saving changes shown above to :", os.path.join(lang, xml_dir, 'mst_ship.xml'))
with open(os.path.join(lang, xml_dir, 'mst_ship.xml'), 'w') as f:
f.write(xmltodict.unparse(shipxml, pretty=True))
# open file again and convert `<Yomi></Yomi>` to `<Yomi />`, which the program expects apparently
with open(os.path.join(lang, xml_dir, 'mst_ship.xml'), 'r') as f:
filedata = f.read()
filedata = filedata.replace('<Yomi></Yomi>', '<Yomi />') # in memory but should be small
with open(os.path.join(lang, xml_dir, 'mst_ship.xml'), 'w') as f:
f.write(filedata)
def slot_items(lang='en'):
# open xml
itemxml = xmltodict.parse(open(os.path.join(jp_xml_dir, 'mst_slotitem.xml'), 'rb'))
# get KC3 JSON, kanji name as key and translation as value
itemlist = json.load(open(os.path.join(kc3_trans_dir, lang, 'items.json'), 'r'))
# replace all names with corresponding unicode string
for item in itemxml['mst_slotitem_data']['mst_slotitem']:
# take kanji name (without trailing words) and match to translation
item['Name'] = itemlist[item['Name']]
print(item['Id'], item['Name'])
# save changes to file
print("Saving changes shown above to :", en_xml_dir + 'mst_slotitem.xml')
with open(os.path.join(lang, xml_dir, 'mst_slotitem.xml'), 'w') as f:
f.write(xmltodict.unparse(itemxml, pretty=True))
# needs to parse from wiki:
#def quests(lang='en'):
## open xml
#xml_fname = 'mst_quest.xml'
#xml = xmltodict.parse(open(os.path.join(jp_xml_dir, xml_fname), 'rb'))
## get KC3 JSON, id as key and translation as value
#list_fname = 'quests.json'
#datalist = json.load(open(os.path.join(kc3_trans_dir, lang, list_fname), 'r'))
## replace all names with corresponding unicode string
#for item in xml['mst_quest_data']['mst_quest']:
## take kanji name (without trailing words) and match to translation
#item['Name'] = datalist[item['Id']]['Name']
#item['Details'] = datalist[item['Id']]['Name']
#print(item['Id'], item['Name'], item['Details'])
## save changes to file
#print("Saving changes shown above to :", os.path.join(lang, xml_dir, xml_fname))
##with open(os.path.join(en_xml_dir, xml_fname), 'w') as f:
## f.write(xmltodict.unparse(xml, pretty=True))
#print(xmltodict.unparse(xml, pretty=True))
# needs to parse from wiki:
def quest_hash(lang='en'):
# open xml
xml_fname = 'mst_quest.xml'
xml = xmltodict.parse(open(os.path.join(jp_xml_dir, xml_fname), 'rb'))
# get KC3 JSON, name as key, id as value
list_fname = 'quests.json'
jp_datalist = json.load(open(os.path.join(kc3_trans_dir, lang, list_fname), 'r'))
name_hash = {}
desc_hash = {}
for key in jp_datalist.keys():
name_hash[jp_datalist[key]['name']] = key
desc_hash[jp_datalist[key]['desc']] = key
list_fname = 'quests.json'
datalist = json.load(open(os.path.join(kc3_en_dir, list_fname), 'r'))
# replace all names with corresponding unicode string
for item in xml['mst_quest_data']['mst_quest']:
# take kanji name (without trailing words) and match to translation
try: # first search for name
# replace weird characters
orig_id = name_hash[item['Name'].replace('第1次', '第一次').replace('第2次', '第二次').replace('1', '1').replace('2', '2').replace('3', '3').replace('4', '4')]
if item['Name'] == '機種転換':
print('KC:', orig_id, 'KCV:', item['Id'], '\n', item['Name'], '\n', item['Details'], '\n', datalist[item['Id']]['name'], '\n', datalist[item['Id']]['desc'])
continue
print('KC:', orig_id, 'KCV:', item['Id'], '\n', item['Name'], '\n', item['Details'], '\n', datalist[orig_id]['name'], '\n', datalist[orig_id]['desc'])
except KeyError:
try: # second search for details
orig_id = desc_hash[item['Details'].replace('1', '1').replace('2', '2').replace('3', '3').replace('4', '4')]
print('KC:', orig_id, 'KCV:', item['Id'], '\n', item['Name'], '\n', item['Details'], '\n', datalist[orig_id]['name'], '\n', datalist[orig_id]['desc'])
except KeyError:
print('KCV:', item['Id'], '\n', item['Name'], '\n', item['Details'])
# save changes to file
print("Saving changes shown above to :", os.path.join(lang, xml_dir, xml_fname))
#with open(os.path.join(lang, xml_dir, xml_fname), 'w') as f:
# f.write(xmltodict.unparse(xml, pretty=True))
#print(xmltodict.unparse(xml, pretty=True))
def stype(lang='en'):
# open xml
xml_fname = 'mst_stype.xml'
xml = xmltodict.parse(open(os.path.join(jp_xml_dir, xml_fname), 'rb'))
# get KC3 JSON, id as key and translation as value
list_fname = 'stype.json'
datalist = json.load(open(os.path.join(kc3_trans_dir, lang, list_fname), 'r'))
# replace all names with corresponding unicode string
for item in xml['mst_stype_data']['mst_stype']:
# take kanji name (without trailing words) and match to translation
item['Name'] = datalist[int(item['Id'])]
print(item['Id'], item['Name'])
# save changes to file
print("Saving changes shown above to :", os.path.join(lang, xml_dir, xml_fname))
with open(os.path.join(lang, xml_dir, xml_fname), 'w') as f:
f.write(xmltodict.unparse(xml, pretty=True))
def quotes(lang='en'):
# open xml
xml_fname = 'mst_shiptext.xml'
xml = xmltodict.parse(open(os.path.join(jp_xml_dir, xml_fname), 'rb'))
# get KC3 JSON, id as key and translation as value
list_fname = 'quotes.json'
datalist = json.load(open(os.path.join(kc3_trans_dir, lang, list_fname), 'r'))
# get original ship name to help
shipxml_fname = 'mst_ship.xml'
shipxml = xmltodict.parse(open(os.path.join(en_xml_dir, shipxml_fname), 'rb'))
# compile a shiphash table with Id as key by processing shiplist XML
shiplist = {}
for item in shipxml['mst_ship_data']['mst_ship']:
shiplist[item['Id']] = item['Name']
# compile a shiphash table with Name as key
shipnames = {}
for item in shipxml['mst_ship_data']['mst_ship']:
shipnames[item['Name']] = item['Id']
# replace all names with corresponding unicode string
for item in xml['mst_shiptext_data']['mst_shiptext']:
if (item['Id'] == '147'): # Verniy
item['Getmes'] = datalist['147']['1'] # retain original getmessage
item['Sinfo'] = datalist['35']['25']
continue
if (item['Id'] == '177'): # Prinz Eugen (not easily found due to space)
item['Getmes'] = datalist['176']['1']
item['Sinfo'] = datalist['176']['25']
continue
if (item['Id'] == '353'): # Graf Zeppelin (not easily found due to space)
item['Getmes'] = datalist['432']['1'] # retain original getmessage
item['Sinfo'] = datalist['432']['25']
continue
elif (item['Id'] == '357') or (item['Id'] == '463'): # Iowa
item['Getmes'] = datalist['440']['1']
item['Sinfo'] = datalist['440']['25']
continue
# Skip null shiptext IDs
if (item['Getmes'] == None) and (item['Sinfo'] == None):
continue
try:
item['Getmes'] = datalist[item['Id']]['1'].replace(r'<br/>', r'\n').replace(r'<br />', r'\n')
except KeyError: # if get message not found, check if kai
pass
try:
item['Sinfo'] = datalist[item['Id']]['25'].replace(r'<br/>', r'\n').replace(r'<br />', r'\n')
except KeyError: # ignore ship IDs with empty slots
# if Sinfo is missing, get base name to find and insert base ship text
fullname = shiplist[item['Id']].split()
try:
base_id = shipnames[fullname[0]] # find basename ID
item['Getmes'] = datalist[base_id]['1'].replace(r'<br/>', r'\n')
item['Sinfo'] = datalist[base_id]['25'].replace('<br/>', r'\n')
except KeyError:
pass
# save changes to file
print("Saving changes shown above to :", os.path.join(lang, xml_dir, xml_fname))
with open(os.path.join(lang, xml_dir, xml_fname), 'w') as f:
f.write(xmltodict.unparse(xml, pretty=True))
# open file again and convert `<Yomi></Yomi>` to `<Yomi />`, which the program expects apparently
with open(os.path.join(lang, xml_dir, xml_fname), 'r') as f:
filedata = f.read()
filedata = re.sub(r'\<(\w+)\>\</(\w+)\>', r'<\1 />', filedata) # in memory but should be small
with open(os.path.join(lang, xml_dir, xml_fname), 'w') as f:
f.write(filedata)
#ships()
#slot_items()
#quests()
#quest_hash()
#stype()
quotes('tcn')
print("Changes compiled. To start over, replace the `Xml/` folder in `en/` with the one from `jp/`.")