This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
example.js
73 lines (53 loc) · 1.91 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
69
70
71
72
73
'use strict';
const translate = require('./');
(async () => {
// English => Chinese
await translate('hello world', { to: 'zh-cn' })
.then(console.info)
.catch(console.error);
console.info('\n\n');
// Chinese => English
await translate('你好世界', { to: 'en' })
.then(console.info)
.catch(console.error);
console.info('\n\n');
// with raw data response
await translate('hello world', { to: 'zh-cn', raw: true })
.then(console.info)
.catch(console.error);
console.info('\n\n');
/**
* 通过 domain 参数修改翻译网址,默认值为 cn
* com => https://translate.google.com
* cn => https://translate.google.cn
* hk => https://translate.google.hk
*/
await translate('hello world', { to: 'zh-cn', domain: 'cn' })
.then(console.info)
.catch(console.error);
})();
// output
// { text: '你好,世界',
// from:
// { language: { didYouMean: false, iso: 'en' },
// text:
// { autoCorrected: false, value: 'hello world', didYouMean: true } },
// raw: '' }
// { text: 'Hello world',
// from:
// { language: { didYouMean: false, iso: 'zh-CN' },
// text: { autoCorrected: false, value: '你好世界', didYouMean: true } },
// raw: '' }
// { text: '你好,世界',
// from:
// { language: { didYouMean: false, iso: 'en' },
// text:
// { autoCorrected: false, value: 'hello world', didYouMean: true } },
// raw:
// '[[["你好,世界","hello world",null,null,1]\n,[null,null,"Nǐ hǎo, shìjiè"]\n]\n,null,"en",null,null,[["hello world",null,[["你好,世界",1000,true,false]\n,["你好世界",1000,true,false]\n]\n,[[0,11]\n]\n,"hello world",0,0]\n]\n,0.8867333,[]\n,[["en"]\n,null,[0.8867333]\n,["en"]\n]\n]\n' }
// { text: '你好,世界',
// from:
// { language: { didYouMean: false, iso: 'en' },
// text:
// { autoCorrected: false, value: 'hello world', didYouMean: true } },
// raw: '' }