-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtools.js
91 lines (85 loc) · 3.01 KB
/
tools.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const axios = require('axios');
/**
* Tests whether the given variable is a real object and not an Array
* @param {any} it The variable to test
* @returns {it is Record<string, any>}
*/
function isObject(it) {
// This is necessary because:
// typeof null === 'object'
// typeof [] === 'object'
// [] instanceof Object === true
return Object.prototype.toString.call(it) === '[object Object]';
}
/**
* Tests whether the given variable is really an Array
* @param {any} it The variable to test
* @returns {it is any[]}
*/
function isArray(it) {
if (typeof Array.isArray === 'function') return Array.isArray(it);
return Object.prototype.toString.call(it) === '[object Array]';
}
/**
* Translates text to the target language. Automatically chooses the right translation API.
* @param {string} text The text to translate
* @param {string} targetLang The target languate
* @param {string} [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
* @returns {Promise<string>}
*/
async function translateText(text, targetLang, yandexApiKey) {
if (targetLang === 'en') {
return text;
}
if (yandexApiKey) {
return await translateYandex(text, targetLang, yandexApiKey);
} else {
return await translateGoogle(text, targetLang);
}
}
/**
* Translates text with Yandex API
* @param {string} text The text to translate
* @param {string} targetLang The target languate
* @param {string} [apiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
* @returns {Promise<string>}
*/
async function translateYandex(text, targetLang, apiKey) {
if (targetLang === 'zh-cn') {
targetLang = 'zh';
}
try {
const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
const response = await axios({url, timeout: 15000});
if (response.data && response.data['text']) {
return response.data['text'][0];
}
throw new Error('Invalid response for translate request');
} catch (e) {
throw new Error(`Could not translate to "${targetLang}": ${e}`);
}
}
/**
* Translates text with Google API
* @param {string} text The text to translate
* @param {string} targetLang The target languate
* @returns {Promise<string>}
*/
async function translateGoogle(text, targetLang) {
try {
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`;
const response = await axios({url, timeout: 15000});
if (isArray(response.data)) {
// we got a valid response
return response.data[0][0][0];
}
throw new Error('Invalid response for translate request');
} catch (e) {
throw new Error(`Could not translate to "${targetLang}": ${e}`);
}
}
module.exports = {
isArray,
isObject,
translateText
};