-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtranslate.py
217 lines (194 loc) · 7.74 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
import json
import os
import re
from pathlib import Path
symbols = [' ', '(', ')', '[', ']', '-', '{', '}', '%', "'", '"', ',']
class RuneTrieNode:
def __init__(self):
self.child = {}
self.value = None
class RuneTrie:
def __init__(self):
self.__root = RuneTrieNode()
def put_if_absent(self, key: str, value) -> bool:
if value is None:
raise ValueError('cannot put a nil value')
key = key.lower()
node = self.__root
for c in key:
n = node.child.get(c)
if n:
node = n
else:
new_node = RuneTrieNode()
node.child[c] = new_node
node = new_node
if node.value:
return False
node.value = value
return True
def __get_longest(self, s: str) -> (str, str):
node2 = None
key2 = ''
node = self.__root
key = ''
for idx in range(len(s)):
c = s[idx]
n = node.child.get(c.lower())
if n:
key += c
node = n
if node.value is not None and (idx + 1 >= len(s) or s[idx + 1] in symbols):
node2 = node
key2 = key
else:
break
if node2:
return key2, node2.value
return '', ''
def replace_all(self, s: str) -> str:
s2 = ''
while s:
if not (len(s2) == 0 or s2[-1] in symbols):
s2 += s[0]
s = s[1:]
continue
key, value = self.__get_longest(s)
if key:
s2 += value
s = s[len(key):]
else:
s2 += s[0]
s = s[1:]
return s2
trie = RuneTrie()
with open('translate.csv', 'r', encoding='utf-8') as f:
f.readline()
while True:
line = f.readline().strip()
if line:
arr = line.split(',')
if not trie.put_if_absent(arr[0], arr[1] if len(arr) >= 2 else ''):
raise ValueError("repeat: " + line)
else:
break
lines = []
regexp = re.compile(r'Description\("(.*?)"\)')
regexp1 = re.compile(r'ToolTip\("(.*?)"\)')
regexp_space = re.compile(r'''(?<![()\[\]{}%'"A-Za-z]) (?![()\[\]{}%'"A-Za-z])''')
with open('hk-split-maker/src/asset/hollowknight/splits.txt', 'r', encoding='utf-8') as f:
while True:
line = f.readline()
if line:
result = regexp.search(line)
if result:
a = result.group(1)
b = regexp_space.sub('', trie.replace_all(a))
line = line.replace(a, b, 1)
result = regexp1.search(line)
if result:
a = result.group(1)
b = regexp_space.sub('', trie.replace_all(a))
line = line.replace(a, b, 1)
lines.append(line)
else:
break
with open('hk-split-maker/src/asset/hollowknight/splits.txt', 'w', encoding='utf-8') as f:
f.writelines(lines)
lines = []
regexp = re.compile(r'return\s+\[.*?(?<=[\[(])["`](.*?)["`].*?];')
regexp1 = re.compile(r'return\s+\[.*?["`](.*?)["`](?=[)\]]).*?];')
start_qualifier = False
regexp2 = re.compile(r'["/](.*?)["/]')
with open('hk-split-maker/src/lib/hollowknight-splits.ts', 'r', encoding='utf-8') as f:
while True:
line = f.readline()
if line:
if not start_qualifier and 'switch (qualifier) {' in line:
start_qualifier = True
if start_qualifier and line[0] == '{':
start_qualifier = False
result = regexp.search(line)
if result:
a = result.group(1)
b = trie.replace_all(a).replace(' ', '')
line = line.replace(a, b, 1)
result = regexp1.search(line)
if result:
a = result.group(1)
b = trie.replace_all(a).replace(' ', '')
line = line.replace(a, b, 1)
if start_qualifier:
result = regexp2.search(line)
if result:
a = result.group(1)
b = trie.replace_all(a).replace(' ', '')
line = line.replace(a, b, 1)
lines.append(line)
else:
break
with open('hk-split-maker/src/lib/hollowknight-splits.ts', 'w', encoding='utf-8') as f:
f.writelines(lines)
file = Path('hk-split-maker/src/components/App.tsx')
content = file.read_text('utf-8')
content = content.replace('<h2>Input Configuration</h2>', '<h2>输入配置</h2>')
content = content.replace('<h2>Output Splits File</h2>', '<h2>输出Splits文件</h2>')
content = content.replace('text="Generate"', 'text="生成"')
content = content.replace('text="Download"', 'text="下载"')
content = content.replace('text="Import Splits"', 'text="导入Splits"')
file.write_text(content, 'utf-8')
file = Path('hk-split-maker/src/components/ShareButton.tsx')
content = file.read_text('utf-8')
content = content.replace('''<span className="button-text">Share</span>''',
'''<span className="button-text">分享</span>''')
file.write_text(content, 'utf-8')
file = Path('hk-split-maker/src/components/CategorySelect.tsx')
content = file.read_text('utf-8')
content = content.replace('"Pre-made Category: Select or type to search..."', '"选择预设模板..."')
file.write_text(content, 'utf-8')
file = Path('hk-split-maker/src/components/SplitSelect.tsx')
content = file.read_text('utf-8')
content = content.replace('"Add autosplit: Select or type to search..."', '"增加片段..."')
file.write_text(content, 'utf-8')
file = Path('hk-split-maker/src/components/AlertBanner.tsx')
content = file.read_text('utf-8')
content = content.replace('''Interested in contributing or suggesting ideas and splits? Check out
the ''',
'''如果您想要为汉化做贡献,欢迎前往
<a href="https://github.com/CuteReimu/hk-split-maker" target="_blank" rel="noopener noreferrer">
我们的汉化Github工程</a>。如果您想要为网页功能或原英文版网页做贡献,欢迎前往''')
content = content.replace('''Need help? Join the ''',
'''如果您需要帮助,欢迎前往''')
content = content.replace('''and check out #tech-support''',
'''内的#tech-support频道。''')
file.write_text(content, 'utf-8')
path = 'hk-split-maker/src/asset/hollowknight/categories'
files = os.listdir(path)
for file_name in files:
file_path = os.path.join(path, file_name)
if file_name == 'category-directory.json':
file = Path(file_path)
content = file.read_text('utf-8')
json_result = json.loads(content)
for d in json_result.values():
for obj in d:
obj['displayName'] = regexp_space.sub('', trie.replace_all(obj['displayName']))
content = json.dumps(json_result, indent=4, ensure_ascii=False)
file.write_text(content, 'utf-8')
elif file_name.endswith('.json'):
file = Path(file_path)
content = file.read_text('utf-8')
json_result = json.loads(content)
if 'names' in json_result:
j = json_result['names']
for k, v in j.items():
if isinstance(v, list):
for i in range(len(v)):
v[i] = regexp_space.sub('', trie.replace_all(v[i]))
else:
j[k] = regexp_space.sub('', trie.replace_all(v))
if 'endingSplit' in json_result:
j = json_result['endingSplit']
j['name'] = regexp_space.sub('', trie.replace_all(j['name']))
content = json.dumps(json_result, indent=4, ensure_ascii=False)
file.write_text(content + '\n', 'utf-8')