-
Notifications
You must be signed in to change notification settings - Fork 46
/
translate.js
68 lines (66 loc) · 2.24 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
import path from 'path';
import fs from 'fs';
import {
contentTemplate,
contentTemplateRule,
supportedService,
targetSrtSaveName,
translateConfig,
translateServiceProvider,
} from './config.js';
import { renderTemplate } from './utils.js';
export default async function translate(folder, fileName, absolutePath) {
const renderContentTemplate = contentTemplate[contentTemplateRule];
return new Promise(async (resolve, reject) => {
try {
const result = fs.readFileSync(absolutePath, 'utf8');
const data = result.split('\n');
const items = [];
for (var i = 0; i < data.length; i += 4) {
const source = data[i + 2];
if (!source) continue;
let text;
switch (translateServiceProvider) {
case supportedService.volc:
const volc = await import('./service/volc.js');
text = await volc.default(source);
break;
case supportedService.baidu:
const baidu = await import('./service/baidu.js');
text = await baidu.default(source);
break;
case supportedService.deeplx:
const deeplx = await import('./service/deeplx.js');
text = await deeplx.default(source);
break;
case supportedService.ollama:
const ollama = await import('./service/ollama.js');
text = await ollama.default(source, translateConfig.sourceLanguage, translateConfig.targetLanguage);
break;
default:
text = 'no supported service';
}
items.push({
id: data[i],
startEndTime: data[i + 1],
targetContent: text,
sourceContent: source,
});
}
const fileSave = path.join(folder, `${renderTemplate(targetSrtSaveName, { fileName, ...translateConfig })}.srt`);
for (let i = 0; i <= items.length - 1; i++) {
const item = items[i];
const content = `${item.id}\n${item.startEndTime}\n${renderTemplate(renderContentTemplate, item)}`;
fs.appendFileSync(fileSave, content, (err) => {
if (err) {
console.error(err);
}
});
}
resolve();
} catch (error) {
console.error(error);
reject(error);
}
});
}