This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathexample.js
68 lines (59 loc) · 2.41 KB
/
example.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
const {
translate,
detectLanguage,
wordAlternatives,
translateWithAlternatives,
} = require('./index');
// Translate text with explicit source and target languages
translate('Die Übersetzungsqualität von deepl ist erstaunlich!', 'EN', 'DE')
.then(res => console.log(`Translation: ${res.translation}`))
.catch(console.error);
// Translate short text with this method to get a few translation alternatives
translateWithAlternatives(
'Die Übersetzungsqualität von deepl ist erstaunlich!',
'EN'
)
.then(res =>
console.log(
`Translation alternatives: ${res.translationAlternatives.join(', ')}`
)
)
.catch(console.error);
// Leave out the source language or specify 'auto' to autodetect the input
translate('This is a representative chunk of text in english.', 'DE')
.then(res => console.log(`Translation: ${res.translation}`))
.catch(console.error);
// Detect the text language without giving back the translation
detectLanguage('Deepl también puede detectar un idioma. ¿Qué idioma es este?')
.then(res => console.log(`Detected language: ${res.languageName}`))
.catch(console.error);
// Multi-line translations with different languages in each paragraph work as well
translate(
`Das ist der erste Satz... Das der zweite.
C'est la troisième phrase.
Y ese es el cuarto.
I piąta.`,
'EN'
)
.then(res => console.log(`Translation: ${res.translation}`))
.catch(console.error);
// This method allows for tweaking the translation. By providing a beginning, we define a word or a phrase
// for which we want alternative translations within the context of a larger body of text (a sentence).
// One of the returned alternatives can then be selected and passed into translateWithAlternatives
// as an overriding word/phrase for the beginning of the whole translation.
{
const text = 'Die Übersetzungsqualität von deepl ist erstaunlich!';
// Translates to: 'The translation quality of deepl is amazing!'
wordAlternatives(text, 'EN', 'DE', 'The translation')
.then(res => {
console.log(`Alternative beginnings:`);
res.alternatives.slice(0, 3).forEach(el => console.log(el));
// Choose the third alternetive
return res.alternatives[2];
})
.then(beginning => {
// Request translation with selected alternative beginning
return translateWithAlternatives(text, 'EN', 'DE', beginning);
})
.then(res => console.log(`Alternative: ${res.translation}`));
}