Skip to content

Commit 6d3306c

Browse files
Add item translation table feature
1 parent f0b3e9e commit 6d3306c

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

auto_translate.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import binascii
2+
import xmltodict
3+
import xml.etree.ElementTree as ET
4+
import glob
5+
import os
6+
7+
from collections import OrderedDict
8+
from config import *
9+
from utils import *
10+
11+
class Item():
12+
itemid = 0
13+
en = ""
14+
jp = ""
15+
16+
def dump_table(autotranslate_table):
17+
output_filename = 'out/sql/item_translation_table.sql'
18+
19+
header = '''-- /translate command table
20+
-- Store as varbinary because of shift-jis shenanigans. English text will be transformed on load.
21+
DROP TABLE IF EXISTS `item_translation_table`;
22+
23+
/*!40101 SET @saved_cs_client = @@character_set_client */;
24+
/*!40101 SET character_set_client = utf8 */;
25+
CREATE TABLE `item_translation_table` (
26+
`itemId` smallint(5) unsigned NOT NULL,
27+
`englishText` varbinary(64) NOT NULL DEFAULT 0,
28+
`japaneseText` varbinary(64) NOT NULL DEFAULT 0,
29+
PRIMARY KEY (`itemId`)
30+
) ENGINE=Aria TRANSACTIONAL=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci PACK_KEYS=1;
31+
32+
'''
33+
with open(output_filename, "w", encoding="utf-8") as file:
34+
file.write(header)
35+
36+
for itemid in sorted(autotranslate_table.keys()):
37+
if not 'jp' in autotranslate_table[itemid] or not 'en' in autotranslate_table[itemid]:
38+
continue # No key, skip it.
39+
40+
# Japanese text doesn't seem to use quotes, but will that always be true?
41+
en = autotranslate_table[itemid]['en'].replace("'", "\\'").replace("\"","\\\"")
42+
jp = autotranslate_table[itemid]['jp'].replace("'", "\\'").replace("\"","\\\"")
43+
44+
file.write(u"INSERT INTO `item_translation_table` VALUES ({0}, '{1}', '{2}');\n".format(itemid, en, jp))
45+
46+
def auto_translate():
47+
# Yuck. polutils doesn't extract generalE2 to the same location.
48+
files_en = { "generalE", "../items-general2", "usableE", "puppetE", "armorE", "armor2E", "weaponE", "slipE", "currencyE", }
49+
files_jp = { "generalJ", "general2J", "usableJ", "puppetJ", "armorJ", "armor2J", "weaponJ", "slipJ", "currencyJ"}
50+
51+
en_items = {}
52+
for file in files_en:
53+
tree = ET.parse('res/resources/' + file + '.xml')
54+
xml = xmltodict.parse(ET.tostring(tree.getroot(), encoding='unicode'))
55+
for item in xml["thing-list"]["thing"]:
56+
parse_fields(en_items, item)
57+
58+
jp_items = {}
59+
for file in files_jp:
60+
tree = ET.parse('res/resources/' + file + '.xml')
61+
xml = xmltodict.parse(ET.tostring(tree.getroot(), encoding='unicode'))
62+
for item in xml["thing-list"]["thing"]:
63+
parse_fields(jp_items, item)
64+
65+
autotranslate_table = {}
66+
67+
for item in en_items:
68+
if item in autotranslate_table:
69+
autotranslate_table[item]['en'] = en_items[item]['name']
70+
else:
71+
autotranslate_table[item] = {}
72+
autotranslate_table[item]['en'] = en_items[item]['name']
73+
74+
for item in jp_items:
75+
if item in autotranslate_table:
76+
autotranslate_table[item]['jp'] = jp_items[item]['name']
77+
else:
78+
autotranslate_table[item] = {}
79+
autotranslate_table[item]['jp'] = jp_items[item]['name']
80+
81+
for at in autotranslate_table:
82+
if not 'jp' in autotranslate_table[at]:
83+
print("Warning: Item id '{0}' named '{1}' not found in jp table. Please investigate for a real error".format(jp_items[at]['id'], jp_items[at]['name']))
84+
continue
85+
if not 'en' in autotranslate_table[at]:
86+
print("Warning: Item id '{0}' named '{1}' not found in en table. This may not be a real problem -- JP dats can sometimes contain extra data.".format(jp_items[at]['id'], jp_items[at]['name'].encode('utf-8')))
87+
continue
88+
89+
dump_table(autotranslate_table)
90+
91+
def parse_fields(items, item):
92+
93+
# no clue. sometimes item is "@type" or "field" strings.
94+
# check if "item" is a string type, do not parse.
95+
if isinstance(item, str):
96+
return
97+
98+
if item["field"][6]["#text"] == ".":
99+
return
100+
101+
items[int(item["field"][0]["#text"])] = {}
102+
try:
103+
for field in item["field"]:
104+
items[int(item["field"][0]["#text"])][field["@name"]] = field["#text"]
105+
except:
106+
pass
107+
108+
# Enable to test just this file
109+
#auto_translate()

update_extractor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from zone_texts import *
6464
from entity_ids import *
6565
from misc import *
66+
from auto_translate import *
6667

6768
############################
6869
# Config
@@ -122,5 +123,6 @@
122123
zone_texts()
123124
#entity_ids()
124125
#misc()
126+
#auto_translate()
125127

126128
print("Done")

0 commit comments

Comments
 (0)