simple-translate/src/common/translate.js

132 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-03-05 05:58:05 +00:00
import browser from "webextension-polyfill";
2019-02-22 16:13:11 +00:00
import log from "loglevel";
2022-03-03 07:24:35 +00:00
import axios from "axios";
2022-03-03 11:46:48 +00:00
import { getSettings } from "src/settings/settings";
2019-02-21 16:43:45 +00:00
let translationHistory = [];
2018-06-02 12:42:29 +00:00
2019-02-22 16:13:11 +00:00
const logDir = "common/translate";
2022-03-03 11:46:48 +00:00
const getHistory = (sourceWord, sourceLang, targetLang, translationApi) => {
2019-02-21 16:43:45 +00:00
const history = translationHistory.find(
history =>
history.sourceWord == sourceWord &&
history.sourceLang == sourceLang &&
history.targetLang == targetLang &&
2022-03-03 11:46:48 +00:00
history.translationApi == translationApi &&
!history.result.isError
2019-02-21 16:43:45 +00:00
);
return history;
};
2022-03-03 11:46:48 +00:00
const setHistory = (sourceWord, sourceLang, targetLang, translationApi, result) => {
2019-02-21 16:43:45 +00:00
translationHistory.push({
sourceWord: sourceWord,
sourceLang: sourceLang,
targetLang: targetLang,
2022-03-03 11:46:48 +00:00
translationApi: translationApi,
result: result
2019-02-21 16:43:45 +00:00
});
};
2018-11-15 12:24:18 +00:00
2022-03-03 11:46:48 +00:00
const sendRequestToGoogle = async (word, sourceLang, targetLang) => {
2019-02-21 16:43:45 +00:00
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLang}&tl=${targetLang}&dt=t&dt=bd&dj=1&q=${encodeURIComponent(
word
)}`;
2022-03-03 07:24:35 +00:00
const result = await axios.get(url).catch(error => error.response);
2019-02-21 16:43:45 +00:00
const resultData = {
resultText: "",
candidateText: "",
sourceLanguage: "",
percentage: 0,
2022-03-03 07:24:35 +00:00
isError: false,
errorMessage: ""
2019-02-21 16:43:45 +00:00
};
2018-06-02 12:29:38 +00:00
2022-03-03 07:24:35 +00:00
if (!result || result?.status !== 200) {
resultData.isError = true;
if (!result || result.status === 0) resultData.errorMessage = browser.i18n.getMessage("networkError");
else if (result.status === 429 || result.status === 503) resultData.errorMessage = browser.i18n.getMessage("unavailableError");
else resultData.errorMessage = `${browser.i18n.getMessage("unknownError")} [${result?.status} ${result?.statusText}]`;
2019-02-22 17:22:13 +00:00
2022-03-03 07:24:35 +00:00
log.error(logDir, "sendRequest()", result);
2019-02-22 16:13:11 +00:00
return resultData;
}
2018-06-02 12:29:38 +00:00
2022-03-03 07:24:35 +00:00
resultData.sourceLanguage = result.data.src;
resultData.percentage = result.data.ld_result.srclangs_confidences[0];
resultData.resultText = result.data.sentences.map(sentence => sentence.trans).join("");
if (result.data.dict) {
resultData.candidateText = result.data.dict
2019-02-21 16:43:45 +00:00
.map(dict => `${dict.pos}${dict.pos != "" ? ": " : ""}${dict.terms.join(", ")}\n`)
.join("");
2018-07-31 11:35:03 +00:00
}
2018-06-02 12:29:38 +00:00
2022-03-03 07:24:35 +00:00
log.log(logDir, "sendRequest()", resultData);
2019-02-21 16:43:45 +00:00
return resultData;
};
2018-06-02 12:29:38 +00:00
2022-03-03 07:24:35 +00:00
const sendRequestToDeepL = async (word, sourceLang, targetLang) => {
let params = new URLSearchParams();
2022-03-03 11:46:48 +00:00
const authKey = getSettings("deeplAuthKey");
params.append("auth_key", authKey);
2022-03-03 07:24:35 +00:00
params.append("text", word);
2022-03-03 11:46:48 +00:00
params.append("target_lang", targetLang);
const url = getSettings("deeplPlan") === "deeplFree" ?
"https://api-free.deepl.com/v2/translate" :
"https://api.deepl.com/v2/translate";
const result = await axios.post(url, params).catch(e => e.response);
2022-03-03 07:24:35 +00:00
2022-03-03 11:46:48 +00:00
const resultData = {
resultText: "",
candidateText: "",
sourceLanguage: "",
percentage: 0,
isError: false,
errorMessage: ""
};
2022-03-03 07:24:35 +00:00
2022-03-03 11:46:48 +00:00
if (!result || result?.status !== 200) {
resultData.isError = true;
if (!result || result.status === 0) resultData.errorMessage = browser.i18n.getMessage("networkError");
else if (result.status === 403) resultData.errorMessage = browser.i18n.getMessage("deeplAuthError");
else resultData.errorMessage = `${browser.i18n.getMessage("unknownError")} [${result?.status} ${result?.statusText}] ${result?.data.message}`;
log.error(logDir, "sendRequestToDeepL()", result);
return resultData;
}
resultData.resultText = result.data.translations[0].text;
resultData.sourceLanguage = result.data.translations[0].detected_source_language.toLowerCase();
resultData.percentage = 1;
log.log(logDir, "sendRequestToDeepL()", resultData);
return resultData;
2022-03-03 07:24:35 +00:00
};
2022-03-03 11:46:48 +00:00
export default async (sourceWord, sourceLang = "auto", targetLang, translationApi) => {
log.log(logDir, "tranlate()", sourceWord, targetLang, translationApi);
2019-02-21 16:43:45 +00:00
sourceWord = sourceWord.trim();
2019-02-22 15:21:55 +00:00
if (sourceWord === "")
return {
resultText: "",
candidateText: "",
sourceLanguage: "en",
percentage: 0,
statusText: "OK"
};
2019-02-21 16:43:45 +00:00
const history = getHistory(sourceWord, sourceLang, targetLang);
if (history) return history.result;
2018-07-31 15:50:10 +00:00
2022-03-03 11:46:48 +00:00
const result = getSettings("translationApi") === "google" ?
await sendRequestToGoogle(sourceWord, sourceLang, targetLang) :
await sendRequestToDeepL(sourceWord, sourceLang, targetLang);
setHistory(sourceWord, sourceLang, targetLang, translationApi, result);
2022-03-03 07:24:35 +00:00
return result;
2019-02-21 16:43:45 +00:00
};