Change not to split line breaks when translating
This commit is contained in:
parent
f1d3187932
commit
85ed092113
|
@ -10,24 +10,13 @@ class Translate {
|
||||||
this.sourceWord = word;
|
this.sourceWord = word;
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(sourceWord, sourceLang = "auto", targetLang) {
|
async translate(sourceWord, sourceLang = "auto", targetLang) {
|
||||||
//改行で分割
|
const result = await this.sendRequest(sourceWord, sourceLang, targetLang);
|
||||||
const sourceLines = sourceWord.trim().split("\n");
|
return this.formatResult(result);
|
||||||
|
|
||||||
let promises = [];
|
|
||||||
for (let sourceLine of sourceLines) {
|
|
||||||
promises.push(this.sendRequest(sourceLine, sourceLang, targetLang));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise(resolve => {
|
|
||||||
Promise.all(promises).then(results => {
|
|
||||||
resolve(this.formatResult(results));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendRequest(word, sourceLang, targetLang) {
|
sendRequest(word, sourceLang, targetLang) {
|
||||||
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLang}&tl=${targetLang}&dt=t&dt=bd&q=${encodeURIComponent(
|
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLang}&tl=${targetLang}&dt=t&dt=bd&dj=1&q=${encodeURIComponent(
|
||||||
word
|
word
|
||||||
)}`;
|
)}`;
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
|
@ -45,7 +34,7 @@ class Translate {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
formatResult(results) {
|
formatResult(result) {
|
||||||
const resultData = {
|
const resultData = {
|
||||||
resultText: "",
|
resultText: "",
|
||||||
candidateText: "",
|
candidateText: "",
|
||||||
|
@ -54,40 +43,17 @@ class Translate {
|
||||||
statusText: ""
|
statusText: ""
|
||||||
};
|
};
|
||||||
|
|
||||||
resultData.statusText = results[0].statusText;
|
resultData.statusText = result.statusText;
|
||||||
if (resultData.statusText !== "OK") return resultData;
|
if (resultData.statusText !== "OK") return resultData;
|
||||||
|
|
||||||
//翻訳元言語を取得
|
resultData.sourceLanguage = result.response.src;
|
||||||
resultData.sourceLanguage = results[0].response[2];
|
resultData.percentage = result.response.confidence;
|
||||||
resultData.percentage = results[0].response[6];
|
resultData.resultText = result.response.sentences.map(sentence => sentence.trans).join("");
|
||||||
|
if (result.response.dict) {
|
||||||
let candidateText = "";
|
resultData.candidateText = result.response.dict
|
||||||
let wordCount = 0;
|
.map(dict => `${dict.pos}${dict.pos != "" ? ": " : ""}${dict.terms.join(", ")}\n`)
|
||||||
let lineCount = 0;
|
.join("");
|
||||||
|
|
||||||
for (const result of results) {
|
|
||||||
lineCount++;
|
|
||||||
|
|
||||||
//翻訳文を取得
|
|
||||||
for (const response of result.response[0]) {
|
|
||||||
resultData.resultText += response[0];
|
|
||||||
}
|
}
|
||||||
resultData.resultText += "\n";
|
|
||||||
|
|
||||||
//訳候補を取得
|
|
||||||
if (result.response[1]) {
|
|
||||||
wordCount++;
|
|
||||||
for (let i = 0; i < result.response[1].length; i++) {
|
|
||||||
const partsOfSpeech = result.response[1][i][0];
|
|
||||||
const candidates = result.response[1][i][1];
|
|
||||||
candidateText += `${partsOfSpeech}${partsOfSpeech != "" ? ": " : ""}${candidates.join(
|
|
||||||
", "
|
|
||||||
)}\n`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//訳候補が一つの単語のみに対して存在するとき返す
|
|
||||||
if (wordCount == 1 && lineCount == 1) resultData.candidateText = candidateText;
|
|
||||||
|
|
||||||
return resultData;
|
return resultData;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue