forked from Draginol/GC4_Localization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCTranslatorUI.py
More file actions
277 lines (210 loc) · 11 KB
/
GCTranslatorUI.py
File metadata and controls
277 lines (210 loc) · 11 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
def install_module(module_name):
"""Install the given module using pip."""
subprocess.check_call([sys.executable, "-m", "pip", "install", module_name])
# List of modules to check
required_modules = ["PyQt5", "openai"]
for module in required_modules:
try:
__import__(module)
except ImportError:
print(f"{module} not found. Installing...")
install_module(module)
import sys
import os
import openai
from xml.etree import ElementTree as ET
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout,
QPushButton, QFileDialog, QComboBox, QWidget, QMessageBox)
from PyQt5.QtCore import QSettings
from PyQt5 import sip
from PyQt5.QtWidgets import QProgressDialog
from PyQt5.QtCore import Qt
import subprocess
import sys
openai.api_key = "Your OPENAI_API_KEY"
class CustomTableWidgetItem(QTableWidgetItem):
def __init__(self, text, file_path, label):
super().__init__(text)
self.file_path = file_path
self.label = label
def __hash__(self):
return hash((self.file_path, self.label))
def __eq__(self, other):
if isinstance(other, CustomTableWidgetItem):
return self.file_path == other.file_path and self.label == other.label
return False
class TranslationApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Translation Tool")
self.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
self.language_box = QComboBox(self)
self.languages = ["English", "French", "German", "Russian", "Spanish", "Italian", "Portuguese", "Polish", "Korean", "Japanese", "Chinese"]
self.language_box.addItems(self.languages)
self.language_box.currentIndexChanged.connect(self.switch_language)
self.table = QTableWidget(self)
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['File Name', 'Label', 'English', 'Translation'])
self.table.itemChanged.connect(self.on_item_changed)
self.load_button = QPushButton("Load English Directory", self)
self.load_button.clicked.connect(self.load_directory)
self.save_button = QPushButton("Save Translations", self)
self.save_button.clicked.connect(self.save_translations)
self.translate_button = QPushButton("Translate", self)
self.translate_button.clicked.connect(self.perform_translation)
layout.addWidget(self.load_button)
layout.addWidget(self.translate_button)
layout.addWidget(self.language_box)
layout.addWidget(self.table)
layout.addWidget(self.save_button)
central_widget = QWidget(self)
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.english_strings = {}
self.changed_items = set()
self.parent_directory = ""
self.settings = QSettings("Stardock", "GC4Translator")
last_directory = self.settings.value("last_directory", "H:\\Projects\\GC4\\GalCiv4\\Game\\Data\\English")
if os.path.exists(last_directory):
self.parent_directory = last_directory
def on_item_changed(self, item):
if item.column() == 3: # Check if the translation column was changed
self.changed_items.add(item)
def save_translations(self):
file_updates = {}
# Group items by file path
for item in self.changed_items:
if sip.isdeleted(item): # Check if the item is still valid
continue
if item.file_path not in file_updates:
file_updates[item.file_path] = []
file_updates[item.file_path].append(item)
num_changed_files = len(file_updates)
if num_changed_files > 3:
# Create a QProgressDialog for saving
progress = QProgressDialog("Saving translations...", None, 0, num_changed_files, self)
progress.setWindowFlags(Qt.Dialog | Qt.FramelessWindowHint)
progress.setWindowModality(Qt.WindowModal) # This will block the main window
progress.show()
current_file_count = 0
# For each file, make a single pass and update translations
for file_path, items in file_updates.items():
tree = ET.parse(file_path)
for item in items:
for elem in tree.findall('StringTable'):
if elem.find('Label').text == item.label:
elem.find('String').text = item.text()
try:
tree.write(file_path)
except PermissionError:
QMessageBox.warning(self, "Error", f"Permission denied when trying to write to {file_path}")
return
current_file_count += 1
if num_changed_files > 3:
# Update the progress dialog
progress.setValue(current_file_count)
QApplication.processEvents() # Process events to update the UI immediately
# Close the progress dialog if it was shown
if num_changed_files > 3:
progress.close()
self.changed_items.clear()
def load_directory(self):
default_directory = "H:\\Projects\\GC4\\GalCiv4\\Game\\Data\\English"
# If self.parent_directory is set, use it; otherwise, use the default_directory
starting_directory = self.parent_directory if self.parent_directory else default_directory
directory = QFileDialog.getExistingDirectory(self, "Select English Directory", starting_directory)
if directory:
self.parent_directory = os.path.dirname(directory)
self.settings.setValue("last_directory", self.parent_directory)
for subdir, _, files in os.walk(directory):
for file in files:
if file.endswith(".xml"):
file_path = os.path.join(subdir, file)
file_name = os.path.basename(file_path)
tree = ET.parse(file_path)
for elem in tree.findall('StringTable'):
label = elem.find('Label').text
string = elem.find('String').text
self.english_strings[(file_name, label)] = (string, file_path) # Store with filename and label
self.populate_table()
def populate_table(self):
self.table.setRowCount(len(self.english_strings))
self.table.itemChanged.disconnect(self.on_item_changed)
for idx, ((file_name, label), (string, file_path)) in enumerate(self.english_strings.items()):
self.table.setItem(idx, 0, QTableWidgetItem(file_name))
self.table.setItem(idx, 1, QTableWidgetItem(label)) # This is for the Label column
self.table.setItem(idx, 2, QTableWidgetItem(string))
self.table.setItem(idx, 3, CustomTableWidgetItem(string, file_path, label)) # Default to English for the translation
self.table.itemChanged.connect(self.on_item_changed)
def switch_language(self):
self.table.itemChanged.disconnect(self.on_item_changed)
lang = self.language_box.currentText()
if lang != "English":
sibling_directory = os.path.join(self.parent_directory, lang)
translations = {}
for subdir, _, files in os.walk(sibling_directory):
for file in files:
if file.endswith(".xml"):
file_path = os.path.join(subdir, file)
tree = ET.parse(file_path)
for elem in tree.findall('StringTable'):
label = elem.find('Label').text
string = elem.find('String').text
translations[label] = (string, file_path)
for idx, (file_name, label) in enumerate(self.english_strings.keys()):
translation, trans_file_path = translations.get(label, (self.english_strings[(file_name, label)][0], self.english_strings[(file_name, label)][1]))
self.table.setItem(idx, 3, CustomTableWidgetItem(translation, trans_file_path, label))
self.table.itemChanged.connect(self.on_item_changed)
def translate_to_language(self, text, target_language):
prompt = f"Succinctly translate this English string into {target_language}: {text}"
try:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
max_tokens=250,
n=1,
stop=None,
temperature=0.7,
)
return response.choices[0].message.content.strip()
except Exception as e:
print(f"Error in getting translation feedback: {e}")
return None
def perform_translation(self):
selected_rows = list(set(item.row() for item in self.table.selectedItems()))
total_rows = len(selected_rows)
# Create a QProgressDialog
if total_rows > 4:
progress = QProgressDialog("Please Wait...", None, 0, total_rows, self)
progress.setWindowFlags(Qt.Dialog | Qt.FramelessWindowHint)
progress.setWindowModality(Qt.WindowModal) # This will block the main window
progress.show()
for idx, row in enumerate(selected_rows):
english_text_item = self.table.item(row, 2) # Assuming the English column is index 2
english_text = english_text_item.text()
target_language = self.language_box.currentText()
translated_text = self.translate_to_language(english_text, target_language)
translation_item = self.table.item(row, 3) # Assuming the Translation column is index 3
if not translation_item: # If the cell is empty
file_path = self.table.item(row, 0).text()
label = self.table.item(row, 1).text()
translation_item = CustomTableWidgetItem("", file_path, label)
self.table.setItem(row, 3, translation_item)
translation_item.setText(translated_text)
# Update the button text to show progress
self.translate_button.setText(f"Translating {idx + 1} of {total_rows} entries")
# Update the progress dialog
if total_rows > 4:
progress.setValue(idx + 1)
QApplication.processEvents() # To update the UI immediately
# Reset the button text after translation
self.translate_button.setText("Translate")
# Close the progress dialog after the operation
if total_rows > 4:
progress.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = TranslationApp()
mainWin.show()
sys.exit(app.exec_())