forked from InfinityLoop1308/PipePipeClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
315 lines (260 loc) · 11.1 KB
/
translator.py
File metadata and controls
315 lines (260 loc) · 11.1 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import openai
import json
from forbiddenfruit import curse
import sys
import config
from concurrent.futures import ThreadPoolExecutor
def filter_list(self, func):
"""
Filter the list based on a given lambda function.
Parameters:
func (function): The lambda function used for filtering.
Returns:
list: The filtered list.
"""
return list(filter(func, self))
def map_list(self, func):
"""
Map the list based on a given lambda function.
Parameters:
func (function): The lambda function used for mapping.
Returns:
list: The mapped list.
"""
return list(map(func, self))
def filter_dict(self, func):
"""
Filter the dictionary based on a given lambda function operating on key-value pairs.
Parameters:
func (function): The lambda function used for filtering (takes key and value as input).
Returns:
dict: The filtered dictionary.
"""
return {k: v for k, v in self.items() if func(k, v)}
# Use forbiddenfruit to add the filter_dict method to the built-in dict class
curse(list, "filter", filter_list)
curse(list, "map", map_list)
curse(dict, "filter", filter_dict)
class Translator:
def __init__(self):
self.client = openai.OpenAI(api_key=config.api_key, base_url=config.base_url)
def translate(self, content, language):
response = self.client.chat.completions.create(
model="gpt-4o-2024-08-06",
response_format={"type": "json_object"},
messages=[
{
"role": "user",
"content": f"Translate the values(not including the keys) in the dict to values-{language}. Always preserve new line tokens like \\n. Output result in json format. \n\n{content}",
}
],
temperature=0,
)
translated_text = response.choices[0].message.content.strip()
# print(translated_text)
return translated_text
def get_translated_dict(self, content, language):
return json.loads(self.translate(content, language))
from bs4 import BeautifulSoup
class XMLHandler:
def __init__(self, file_path=None):
self.file_path = file_path
self.soup = None
if file_path:
self.load_from_file(file_path)
def load_from_file(self, file_path):
with open(file_path, 'r') as file:
self.soup = BeautifulSoup(file, 'xml')
self.file_path = file_path
def add_entry(self, parent_selector, new_tag, attributes=None, text=None):
parent = self.soup.select_one(parent_selector)
if parent is None:
raise Exception(f"The parent with selector {parent_selector} does not exist.")
new_element = self.soup.new_tag(new_tag)
if attributes:
new_element.attrs = attributes
if text:
new_element.string = text
parent.append(new_element)
parent.append("\n")
def update_entry(self, selector, new_text=None):
element = self.soup.select_one(selector)
if element is None:
raise Exception(f"The element with selector {selector} does not exist.")
if new_text is not None:
element.string = new_text
def delete_entry(self, selector):
element = self.soup.select_one(selector)
if element:
element.decompose()
def write_to_file(self, file_path=None):
if file_path is None:
if self.file_path:
file_path = self.file_path
else:
raise Exception("No file path specified for writing XML data.")
with open(file_path, 'w') as file:
file.write(str(self.soup))
def load_strings_to_dict(self, tail_length=0):
strings_dict = {}
string_tags = self.soup.find_all('string')
for tag in string_tags:
name = tag.get('name')
if name:
strings_dict[name] = tag.text
return dict(list(strings_dict.items())[-1 * tail_length:])
def load_strings_to_dict_by_part(self):
strings_dict = {}
string_tags = self.soup.find_all('string')
result = []
start = 0
while start < len(string_tags):
for tag in string_tags:
name = tag.get('name')
if name:
strings_dict[name] = tag.text
result.append(dict(list(strings_dict.items())[start:start + 100]))
start += 100
return result
def escape(text):
result = ''
i = 0
while i < len(text):
if text[i] == '\\' and i + 1 < len(text) and text[i + 1] == "'":
result += "\\'"
i += 2
elif text[i] == "'":
result += "\\'"
i += 1
else:
result += text[i]
i += 1
return result.replace("\n", "\\n")
class StringTranslator:
def __init__(self):
self.base = XMLHandler('app/src/main/res/values/strings.xml')
self.targets = [
XMLHandler('app/src/main/res/values-zh-rCN/strings.xml'),
XMLHandler('app/src/main/res/values-zh-rTW/strings.xml'),
XMLHandler('app/src/main/res/values-ja/strings.xml'),
XMLHandler('app/src/main/res/values-vi/strings.xml'),
XMLHandler('app/src/main/res/values-fr/strings.xml'),
XMLHandler('app/src/main/res/values-de/strings.xml'),
XMLHandler('app/src/main/res/values-it/strings.xml'),
XMLHandler('app/src/main/res/values-es/strings.xml'),
]
self.translator = Translator()
def translate_latest_updates_to_all(self, length, ignore_update=False):
updates = self.base.load_strings_to_dict(tail_length=int(length))
def translate_and_update_target(target):
result = self.translator.get_translated_dict(updates, target.file_path.split('/')[-2][7:])
if ignore_update:
print(result)
return
for key, value in result.items():
try :
target.update_entry(f'string[name="{key}"]', escape(value))
except:
target.add_entry('resources', 'string', {'name': key}, escape(value))
target.write_to_file()
# Process translations in parallel
with ThreadPoolExecutor() as executor:
executor.map(translate_and_update_target, self.targets)
def translate_everything(self):
data = self.base.load_strings_to_dict_by_part()
for target in self.targets:
for updates in data:
result = self.translator.get_translated_dict(updates, target.file_path.split('/')[-2][7:])
for key, value in result.items():
target.add_entry('resources', 'string', {'name': key}, escape(value))
target.write_to_file()
def translate_item_updates_to_all(self, item_list):
updates = self.base.load_strings_to_dict().filter(lambda k, v: k in item_list)
def translate_and_update_target(target):
result = self.translator.get_translated_dict(updates, target.file_path.split('/')[-2][7:])
for key, value in result.items():
try :
target.update_entry(f'string[name="{key}"]', escape(value))
except:
target.add_entry('resources', 'string', {'name': key}, escape(value))
target.write_to_file()
# Process translations in parallel
with ThreadPoolExecutor() as executor:
executor.map(translate_and_update_target, self.targets)
def add_new_entry(self, name, value):
self.base.add_entry('resources', 'string', {'name': name}, value)
self.base.write_to_file()
updates = {name: value}
def translate_and_update(target):
result = self.translator.get_translated_dict(updates, target.file_path.split('/')[-2][7:])
target.add_entry('resources', 'string', {'name': name}, escape(result[name]))
target.write_to_file()
# Process translations in parallel
with ThreadPoolExecutor() as executor:
executor.map(translate_and_update, self.targets)
def translate_new_entries(self, item_list):
updates = self.base.load_strings_to_dict().filter(lambda k, v: k in item_list)
for target in self.targets:
result = self.translator.get_translated_dict(updates, target.file_path.split('/')[-2][7:])
for key, value in result.items():
target.add_entry('resources', 'string', {'name': key}, escape(value))
target.write_to_file()
def delete_entry(self, name):
self.base.delete_entry(f'string[name="{name}"]')
self.base.write_to_file()
for target in self.targets:
target.delete_entry(f'string[name="{name}"]')
target.write_to_file()
def update_translations_from_xml(self, input_xml_path, language):
"""
Update translations in an existing XML file with entries from an input XML file.
Parameters:
input_xml_path (str): Path to the input XML file containing new translations
language (str): Target language code (e.g., 'zh-rCN', 'fr', 'de')
Returns:
bool: True if successful, False otherwise
"""
try:
# Load the input XML file
input_xml = XMLHandler(input_xml_path)
input_strings = input_xml.load_strings_to_dict()
# Find the target XML file for the specified language
target = None
for xml_handler in self.targets:
if language in xml_handler.file_path:
target = xml_handler
break
if not target:
print(f"Error: No translation file found for language '{language}'")
return False
# Load existing translations
existing_strings = target.load_strings_to_dict()
# Find common entries between input and existing translations
common_keys = set(input_strings.keys()) & set(existing_strings.keys())
# Update the entries in the target XML
for key in common_keys:
target.update_entry(f'string[name="{key}"]', escape(input_strings[key]))
# Save the updated XML
target.write_to_file()
print(f"Successfully updated {len(common_keys)} translations for language '{language}'")
return True
except Exception as e:
print(f"Error updating translations: {str(e)}")
return False
if __name__ == '__main__':
translator = StringTranslator()
# translator.update_translations_from_xml('tmp.xml', 'ja')
# read params
args = sys.argv
if args[1] == 'add':
translator.add_new_entry(args[2], args[3])
elif args[1] == 'delete' or args[1] == 'remove':
translator.delete_entry(args[2])
elif args[1] == 'update':
translator.translate_item_updates_to_all(args[2:])
elif args[1] == 'add_multi':
translator.translate_new_entries(args[2:])
elif args[1] == 'translate':
translator.translate_everything()
else:
translator.translate_latest_updates_to_all(args[1])