simple-translate/src/common/translate.js

102 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-02-22 16:13:11 +00:00
import log from "loglevel";
2022-03-03 07:24:35 +00:00
import axios from "axios";
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";
2019-02-21 16:43:45 +00:00
const getHistory = (sourceWord, sourceLang, targetLang) => {
const history = translationHistory.find(
history =>
history.sourceWord == sourceWord &&
history.sourceLang == sourceLang &&
history.targetLang == targetLang &&
history.result.statusText == "OK"
);
return history;
};
2019-02-21 16:43:45 +00:00
const setHistory = (sourceWord, sourceLang, targetLang, formattedResult) => {
translationHistory.push({
sourceWord: sourceWord,
sourceLang: sourceLang,
targetLang: targetLang,
result: formattedResult
});
};
2018-11-15 12:24:18 +00:00
2022-03-03 07:24:35 +00:00
const sendRequest = 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) => {
log.log(logDir, "sendRequestToDeepL()");
let params = new URLSearchParams();
const key = "f5a2c02c-7871-af5c-0d6a-244a9e6d4a1f:fx";
params.append("auth_key", key);
params.append("text", word);
params.append("target_lang", "ja");
const url = "https://api-free.deepl.com/v2/translate";
const res = await axios.post(url, params).catch(e => e.response);
console.log("!!!!!!!!!!!!!!!", res);
};
2019-02-21 16:43:45 +00:00
export default async (sourceWord, sourceLang = "auto", targetLang) => {
2019-02-22 16:13:11 +00:00
log.log(logDir, "tranlate()", sourceWord, targetLang);
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
2019-02-21 16:43:45 +00:00
const result = await sendRequest(sourceWord, sourceLang, targetLang);
2022-03-03 07:24:35 +00:00
setHistory(sourceWord, sourceLang, targetLang, result);
return result;
2019-02-21 16:43:45 +00:00
};