forked from mayeaux/generate-subtitles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibreTranslateWrapper.js
63 lines (51 loc) · 1.43 KB
/
libreTranslateWrapper.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
const fetch = require('node-fetch');
const l = console.log;
// TODO: replace this with new instance
const LTHost = process.env.LIBRETRANSLATE;
l('LTHost');
l(LTHost)
const endpoint = LTHost + '/translate';
l('endpoint');
l(endpoint)
process.on('unhandledRejection', (reason, promise) => {
l(reason);
l(promise);
});
async function hitLTBackend({ text, sourceLanguage, targetLanguage }){
const res = await fetch(endpoint, {
method: "POST",
body: JSON.stringify({
q: text,
source: sourceLanguage,
target: targetLanguage
}),
headers: { "Content-Type": "application/json" }
});
return await res.json()
}
async function translateText({ text, sourceLanguage, targetLanguage }){
const translatedResponse = await hitLTBackend({ text, sourceLanguage, targetLanguage });
// l(translatedResponse);
const { translatedText, detectedLanguage } = translatedResponse;
return translatedText;
}
// /** all languages should be as abbreviation **/
//
// // translate from this language
// const sourceLanguage = 'auto';
//
// // into this language
// const targetLanguage = 'es';
//
// const text = 'This is the text I want to translate';
//
// // translate({ text, sourceLanguage, targetLanguage });
//
// async function main(){
// const translatedText = await translateText({ text, sourceLanguage, targetLanguage });
// l('translatedText');
// l(translatedText);
// }
//
// // main();
module.exports = translateText;