1
- const axios = require ( " axios" ) . default ;
1
+ const axios = require ( ' axios' ) . default ;
2
2
3
3
/**
4
4
* Tests whether the given variable is a real object and not an Array
5
5
* @param {any } it The variable to test
6
6
* @returns {it is Record<string, any> }
7
7
*/
8
8
function isObject ( it ) {
9
- // This is necessary because:
10
- // typeof null === 'object'
11
- // typeof [] === 'object'
12
- // [] instanceof Object === true
13
- return Object . prototype . toString . call ( it ) === " [object Object]" ;
9
+ // This is necessary because:
10
+ // typeof null === 'object'
11
+ // typeof [] === 'object'
12
+ // [] instanceof Object === true
13
+ return Object . prototype . toString . call ( it ) === ' [object Object]' ;
14
14
}
15
15
16
16
/**
@@ -19,8 +19,8 @@ function isObject(it) {
19
19
* @returns {it is any[] }
20
20
*/
21
21
function isArray ( it ) {
22
- if ( typeof Array . isArray === " function" ) return Array . isArray ( it ) ;
23
- return Object . prototype . toString . call ( it ) === " [object Array]" ;
22
+ if ( typeof Array . isArray === ' function' ) return Array . isArray ( it ) ;
23
+ return Object . prototype . toString . call ( it ) === ' [object Array]' ;
24
24
}
25
25
26
26
/**
@@ -31,16 +31,16 @@ function isArray(it) {
31
31
* @returns {Promise<string> }
32
32
*/
33
33
async function translateText ( text , targetLang , yandexApiKey ) {
34
- if ( targetLang === "en" ) {
35
- return text ;
36
- } else if ( ! text ) {
37
- return "" ;
38
- }
39
- if ( yandexApiKey ) {
40
- return translateYandex ( text , targetLang , yandexApiKey ) ;
41
- } else {
42
- return translateGoogle ( text , targetLang ) ;
43
- }
34
+ if ( targetLang === 'en' ) {
35
+ return text ;
36
+ } else if ( ! text ) {
37
+ return '' ;
38
+ }
39
+ if ( yandexApiKey ) {
40
+ return translateYandex ( text , targetLang , yandexApiKey ) ;
41
+ } else {
42
+ return translateGoogle ( text , targetLang ) ;
43
+ }
44
44
}
45
45
46
46
/**
@@ -51,19 +51,19 @@ async function translateText(text, targetLang, yandexApiKey) {
51
51
* @returns {Promise<string> }
52
52
*/
53
53
async function translateYandex ( text , targetLang , apiKey ) {
54
- if ( targetLang === "zh-cn" ) {
55
- targetLang = "zh" ;
56
- }
57
- try {
58
- const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${ apiKey } &text=${ encodeURIComponent ( text ) } &lang=en-${ targetLang } ` ;
59
- const response = await axios ( { url, timeout : 15000 } ) ;
60
- if ( response . data && response . data . text && isArray ( response . data . text ) ) {
61
- return response . data . text [ 0 ] ;
62
- }
63
- throw new Error ( "Invalid response for translate request" ) ;
64
- } catch ( e ) {
65
- throw new Error ( `Could not translate to "${ targetLang } ": ${ e } ` ) ;
54
+ if ( targetLang === 'zh-cn' ) {
55
+ targetLang = 'zh' ;
56
+ }
57
+ try {
58
+ const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${ apiKey } &text=${ encodeURIComponent ( text ) } &lang=en-${ targetLang } ` ;
59
+ const response = await axios ( { url, timeout : 15000 } ) ;
60
+ if ( response . data && response . data . text && isArray ( response . data . text ) ) {
61
+ return response . data . text [ 0 ] ;
66
62
}
63
+ throw new Error ( 'Invalid response for translate request' ) ;
64
+ } catch ( e ) {
65
+ throw new Error ( `Could not translate to "${ targetLang } ": ${ e } ` ) ;
66
+ }
67
67
}
68
68
69
69
/**
@@ -73,27 +73,27 @@ async function translateYandex(text, targetLang, apiKey) {
73
73
* @returns {Promise<string> }
74
74
*/
75
75
async function translateGoogle ( text , targetLang ) {
76
- try {
77
- const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${ targetLang } &dt=t&q=${ encodeURIComponent ( text ) } &ie=UTF-8&oe=UTF-8` ;
78
- const response = await axios ( { url, timeout : 15000 } ) ;
79
- if ( isArray ( response . data ) ) {
80
- // we got a valid response
81
- return response . data [ 0 ] [ 0 ] [ 0 ] ;
82
- }
83
- throw new Error ( "Invalid response for translate request" ) ;
84
- } catch ( e ) {
85
- if ( e . response && e . response . status === 429 ) {
86
- throw new Error (
87
- `Could not translate to "${ targetLang } ": Rate-limited by Google Translate`
88
- ) ;
89
- } else {
90
- throw new Error ( `Could not translate to "${ targetLang } ": ${ e } ` ) ;
91
- }
76
+ try {
77
+ const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${ targetLang } &dt=t&q=${ encodeURIComponent ( text ) } &ie=UTF-8&oe=UTF-8` ;
78
+ const response = await axios ( { url, timeout : 15000 } ) ;
79
+ if ( isArray ( response . data ) ) {
80
+ // we got a valid response
81
+ return response . data [ 0 ] [ 0 ] [ 0 ] ;
82
+ }
83
+ throw new Error ( 'Invalid response for translate request' ) ;
84
+ } catch ( e ) {
85
+ if ( e . response && e . response . status === 429 ) {
86
+ throw new Error (
87
+ `Could not translate to "${ targetLang } ": Rate-limited by Google Translate`
88
+ ) ;
89
+ } else {
90
+ throw new Error ( `Could not translate to "${ targetLang } ": ${ e } ` ) ;
92
91
}
92
+ }
93
93
}
94
94
95
95
module . exports = {
96
- isArray,
97
- isObject,
98
- translateText
96
+ isArray,
97
+ isObject,
98
+ translateText
99
99
} ;
0 commit comments