forked from Draginol/GC4_Localization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCTranslateFile.py
More file actions
251 lines (193 loc) · 10.1 KB
/
GCTranslateFile.py
File metadata and controls
251 lines (193 loc) · 10.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
import os
import sys
import subprocess
import openai
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout,
QPushButton, QFileDialog, QComboBox, QWidget, QMessageBox, QInputDialog, QProgressDialog)
from PyQt5.QtCore import QSettings, Qt
from PyQt5 import sip
from lxml import etree as ET # Use lxml's ElementTree API
import html
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", "lxml"]
for module in required_modules:
try:
__import__(module)
except ImportError:
print(f"{module} not found. Installing...")
install_module(module)
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(3)
self.table.setHorizontalHeaderLabels(['File Name', 'Label', 'String'])
self.table.itemChanged.connect(self.on_item_changed)
self.load_button = QPushButton("Load English XML File", self)
self.load_button.clicked.connect(self.load_single_file)
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
# Add the OpenAI Key button
self.openai_key_button = QPushButton("Enter OpenAI Key", self)
self.openai_key_button.clicked.connect(self.set_openai_key)
layout.addWidget(self.openai_key_button)
# Retrieve and set the openai key if it exists
openai_key = self.settings.value("openai_key", None)
if openai_key:
openai.api_key = openai_key
def load_single_file(self):
# Clear the table and the english_strings dictionary
self.table.setRowCount(0)
self.english_strings.clear()
starting_directory = self.parent_directory if self.parent_directory else "H:\\Projects\\GC4\\GalCiv4\\Game\\Data\\English"
file_path, _ = QFileDialog.getOpenFileName(self, "Select XML File", starting_directory, "XML Files (*.xml)")
if file_path:
self.parent_directory = os.path.dirname(file_path)
self.settings.setValue("last_directory", file_path)
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 set_openai_key(self):
# Open an input dialog to get the OpenAI key
key, ok = QInputDialog.getText(self, 'OpenAI Key', 'Enter your OpenAI key:')
if ok:
# Set the key in the openai library
openai.api_key = key
# Store the key using QSettings
self.settings.setValue("openai_key", key)
# Optional: You can display a message saying the key has been set
QMessageBox.information(self, "Success", "OpenAI Key has been set successfully!")
def on_item_changed(self, item):
if item.column() == 2: # Check if the translation column was changed
self.changed_items.add(item)
def save_translations(self):
import html
if not self.changed_items:
QMessageBox.information(self, "Info", "No changes detected to save.")
return
# Extract file_path from the first item (since all items should have the same file path)
file_path = next(iter(self.changed_items)).file_path
parser = ET.XMLParser(remove_blank_text=True)
tree = ET.parse(file_path, parser)
for item in self.changed_items:
# Check if the item is still valid
if sip.isdeleted(item):
continue
for elem in tree.findall('StringTable'):
if elem.find('Label').text == item.label:
fixed_text = html.unescape(item.text())
elem.find('String').text = fixed_text
try:
with open(file_path, "wb") as f:
f.write(ET.tostring(tree, pretty_print=True, encoding='utf-8', xml_declaration=True))
# Inform the user that save was successful
QMessageBox.information(self, "Success", f"Translations saved successfully to {file_path}")
except PermissionError:
QMessageBox.warning(self, "Error", f"Permission denied when trying to write to {file_path}")
return
except Exception as e: # Catch any other exceptions
QMessageBox.warning(self, "Error", f"Error when trying to write to {file_path}: {str(e)}")
return
self.changed_items.clear()
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, CustomTableWidgetItem(file_name, file_path, label))
self.table.setItem(idx, 1, CustomTableWidgetItem(label, file_path, label))
self.table.setItem(idx, 2, CustomTableWidgetItem(string, file_path, label))
self.table.itemChanged.connect(self.on_item_changed)
def translate_to_language(self, text, target_language):
prompt = f"Translate this English string without using more words 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) # Corrected column index for English
english_text = english_text_item.text()
target_language = self.language_box.currentText()
translated_text = self.translate_to_language(english_text, target_language)
# Check if translated_text is None (error occurred during translation)
if translated_text is None:
QMessageBox.warning(self, "Translation Error", f"Failed to translate the string: {english_text}")
continue # Skip the current loop iteration
translation_item = CustomTableWidgetItem(translated_text, english_text_item.file_path, english_text_item.label)
self.table.setItem(row, 2, translation_item) # Overwrite the existing English text with the translation
# 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_())