-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.js
323 lines (323 loc) · 8.71 KB
/
translate.js
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
316
317
318
319
320
321
322
323
// translate.js
class TranslateApp
{
constructor()
{
this.els = this.getElements();
this.state = {
abortCtrl: null,
translatedBlob: null,
translatedFileName: null,
isTranslating: false,
reqQueue: [],
lastReqTime: 0,
};
this.batchSize = this.getNumSetting('translation_batch_size', 10);
this.expRetry = this.getNumSetting('exponential_retry', 10);
this.rateLimit = this.getNumSetting('translation_batch_rpm', 0);
}
getElements()
{
return {
apiModel: document.getElementById('apiModel'),
docFile: document.getElementById('documentFile'),
downloadBtn: document.getElementById('downloadTranslated'),
langSelect: document.getElementById('language'),
progressBar: document.getElementById('translateProgress'),
progressInner: document.querySelector('#translateProgress .progress-bar'),
translateBtn: document.getElementById('translateBtn')
};
}
init()
{
this.loadSettings();
this.setupEvents();
}
loadSettings()
{
const savedModel = StorageService.load('selected_api_model', 'openai');
if (this.els.apiModel)
{
this.els.apiModel.value = savedModel;
}
if (this.els.langSelect)
{
this.els.langSelect.value = StorageService.load('selected_language', 'English');
}
}
setupEvents()
{
this.els.apiModel?.addEventListener('change', this.handleApiModelChange.bind(this));
this.els.downloadBtn?.addEventListener('click', this.downloadFile.bind(this));
this.els.langSelect?.addEventListener('change', this.handleLangChange.bind(this));
this.els.translateBtn?.addEventListener('click', this.handleTranslateClick.bind(this));
}
handleApiModelChange()
{
const model = this.els.apiModel.value;
StorageService.save('selected_api_model', model);
this.batchSize = this.getNumSetting('translation_batch_size', 10);
this.expRetry = this.getNumSetting('exponential_retry', 10);
this.rateLimit = this.getNumSetting('translation_batch_rpm', 0);
}
handleLangChange()
{
StorageService.save('selected_language', this.els.langSelect.value);
}
handleTranslateClick()
{
if (this.state.isTranslating)
{
this.abortTranslation();
}
else
{
this.startTranslation();
}
}
updateProgress(current, total)
{
const percent = Math.round((current / total) * 100);
this.els.progressInner.style.width = `${percent}%`;
this.els.progressInner.setAttribute('aria-valuenow', percent);
this.els.progressInner.textContent = `${percent}%`;
this.els.progressInner.classList.add('progress-bar-animated');
}
showProgress()
{
this.els.progressBar.style.display = 'block';
this.updateProgress(0, 100);
}
hideProgress()
{
this.els.progressBar.style.display = 'none';
}
showDownloadBtn()
{
this.els.downloadBtn.style.display = 'block';
}
hideDownloadBtn()
{
this.els.downloadBtn.style.display = 'none';
}
setTranslateButtonState(isTranslating)
{
this.els.translateBtn.style.backgroundColor = isTranslating ? 'red' : '';
this.els.translateBtn.textContent = isTranslating ? 'Stop Translation' : 'Start Translation';
this.els.translateBtn.dataset.translating = isTranslating;
this.state.isTranslating = isTranslating;
}
getNumSetting(key, defValue, min = null, max = null)
{
let value = parseInt(StorageService.load(key, defValue), 10);
if (isNaN(value))
{
value = defValue;
}
if (min !== null && value < min)
{
value = min;
}
if (max !== null && value > max)
{
value = max;
}
return value;
}
downloadFile()
{
if (!this.state.translatedBlob || !this.state.translatedFileName)
{
return;
}
const url = URL.createObjectURL(this.state.translatedBlob);
const link = document.createElement('a');
link.href = url;
link.download = this.state.translatedFileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
async startTranslation()
{
const file = this.els.docFile.files[0];
if (!file)
{
alert('Please select a DOCX file.');
return;
}
const apiModel = this.els.apiModel.value;
const apiKey = StorageService.load(CONFIG.API.KEYS[apiModel]);
if (!apiKey)
{
alert(`Please set your API key for ${apiModel} in settings.`);
return;
}
this.state.abortCtrl = new AbortController();
this.showProgress();
this.hideDownloadBtn();
this.setTranslateButtonState(true);
const translatedData = await this.processDocx(file, apiModel, apiKey);
this.state.translatedBlob = translatedData.blob;
this.state.translatedFileName = translatedData.fileName;
this.showDownloadBtn();
this.downloadFile();
this.setTranslateButtonState(false);
this.hideProgress();
this.state.abortCtrl = null;
this.state.reqQueue = [];
this.state.lastReqTime = 0;
}
abortTranslation()
{
this.state.abortCtrl?.abort();
this.state.reqQueue = [];
this.state.lastReqTime = 0;
this.setTranslateButtonState(false);
this.hideProgress();
}
async processDocx(file, apiModel, apiKey)
{
const zip = new JSZip();
const docxData = await zip.loadAsync(file);
if (!docxData.file("word/document.xml"))
{
alert("Invalid DOCX file: 'word/document.xml' not found.");
return;
}
const docXml = await docxData.file("word/document.xml")
.async("string");
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(docXml, "application/xml");
const textElems = Array.from(xmlDoc.getElementsByTagNameNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", 't'));
if (textElems.length === 0)
{
alert("No text elements found in the document.");
return;
}
const batches = this.createBatches(textElems, this.batchSize);
await this.processBatches(batches, xmlDoc, apiModel, apiKey);
const serializer = new XMLSerializer();
const modifiedDocXml = serializer.serializeToString(xmlDoc);
docxData.file("word/document.xml", modifiedDocXml);
const origName = file.name;
const baseName = origName.substring(0, origName.lastIndexOf('.'));
const ext = origName.substring(origName.lastIndexOf('.'));
const translatedName = `${baseName} ${this.els.langSelect.value}${ext}`;
const translatedBlob = await docxData.generateAsync(
{
type: "blob",
compression: "DEFLATE",
compressionOptions:
{
level: 9
}
});
return {
blob: translatedBlob,
fileName: translatedName
};
}
createBatches(elements, batchSize)
{
const batches = [];
for (let i = 0; i < elements.length; i += batchSize)
{
batches.push(elements.slice(i, i + batchSize));
}
return batches;
}
async processBatches(batches, xmlDoc, apiModel, apiKey)
{
let processedElems = 0;
const totalElems = batches.reduce((acc, batch) => acc + batch.length, 0);
for (const batch of batches)
{
if (this.state.abortCtrl.signal.aborted)
{
return;
}
const translatePromises = batch.map(async (element) =>
{
return this.translateElement(element, xmlDoc, apiModel, apiKey)
.then(() =>
{
processedElems++;
this.updateProgress(processedElems, totalElems);
});
});
await Promise.all(translatePromises);
}
}
async translateElement(element, xmlDoc, apiModel, apiKey)
{
const origText = element.textContent.trim();
if (!origText) return;
const targetLang = this.els.langSelect.value;
const prompt = `${CONFIG.UI.TRANSLATION_PROMPT} ${targetLang}. ${CONFIG.UI.NO_BS_PROMPT}.\n\n${origText}`;
const translatedText = await this.getTranslatedText(prompt, apiModel, apiKey);
while (element.firstChild)
{
element.removeChild(element.firstChild);
}
element.appendChild(xmlDoc.createTextNode(translatedText));
}
async getTranslatedText(prompt, apiModel, apiKey)
{
const apiCall = () => AiService.generate(prompt, apiModel,
{
abortSignal: this.state.abortCtrl.signal
});
const response = await this.rateLimitRequests(apiCall);
return CONFIG.API.CONFIG.COMPLETION[apiModel].extractContent(response) || "[Translation Failed]";
}
async rateLimitRequests(apiCall)
{
if (this.rateLimit === 0)
{
return await apiCall();
}
return new Promise((resolve, reject) =>
{
this.state.reqQueue.push(
{
apiCall,
resolve,
reject
});
this.processQueue();
});
}
async processQueue()
{
if (this.state.reqQueue.length === 0 || this.state.isTranslating === false)
{
return;
}
const now = Date.now();
const timeSinceLastReq = now - this.state.lastReqTime;
const delayNeeded = (60000 / this.rateLimit) - timeSinceLastReq;
if (delayNeeded > 0)
{
await new Promise(resolve => setTimeout(resolve, delayNeeded));
}
this.state.lastReqTime = Date.now();
const
{
apiCall,
resolve
} = this.state.reqQueue.shift();
const result = await apiCall();
resolve(result);
if (this.state.reqQueue.length > 0)
{
setTimeout(() => this.processQueue(), 0);
}
}
}
document.addEventListener('DOMContentLoaded', () =>
{
const translateApp = new TranslateApp();
translateApp.init();
});