Skip to content

Commit

Permalink
Updated lib/tools.js
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Nov 14, 2021
1 parent f75bb58 commit 4a60e3b
Showing 1 changed file with 53 additions and 45 deletions.
98 changes: 53 additions & 45 deletions lib/tools.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const axios = require("axios");
const axios = require('axios').default;

/**
* 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]";
// This is necessary because:
// typeof null === 'object'
// typeof [] === 'object'
// [] instanceof Object === true
return Object.prototype.toString.call(it) === '[object Object]';
}

/**
Expand All @@ -19,8 +19,8 @@ function isObject(it) {
* @returns {it is any[]}
*/
function isArray(it) {
if (typeof Array.isArray === "function") return Array.isArray(it);
return Object.prototype.toString.call(it) === "[object Array]";
if (typeof Array.isArray === 'function') return Array.isArray(it);
return Object.prototype.toString.call(it) === '[object Array]';
}

/**
Expand All @@ -31,37 +31,39 @@ function isArray(it) {
* @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);
}
if (targetLang === 'en') {
return text;
} else if (!text) {
return '';
}
if (yandexApiKey) {
return translateYandex(text, targetLang, yandexApiKey);
} else {
return 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
* @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}`);
}
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 && isArray(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}`);
}
}

/**
Expand All @@ -71,21 +73,27 @@ async function translateYandex(text, targetLang, apiKey) {
* @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}`);
}
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) {
if (e.response && e.response.status === 429) {
throw new Error(
`Could not translate to "${targetLang}": Rate-limited by Google Translate`
);
} else {
throw new Error(`Could not translate to "${targetLang}": ${e}`);
}
}
}

module.exports = {
isArray,
isObject,
translateText
};
isArray,
isObject,
translateText
};

0 comments on commit 4a60e3b

Please sign in to comment.