Multilanguage support for Sõnaveeb

This commit is contained in:
Bohdan Horbeshko 2024-11-21 02:31:43 +01:00
parent 4870df537c
commit e755656fcd
3 changed files with 38 additions and 8 deletions

View file

@ -3,7 +3,7 @@ const alphabeticallySort = (a, b) => a.name.localeCompare(b.name);
const langListGoogle = ["af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh-CN", "zh-TW", "co", "hr", "cs", "da", "nl", "en", "eo", "et", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko", "ku", "ky", "lo", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "yo", "zu"];
const langListDeepl = ["bg", "cs", "da", "de", "el", "en-GB", "en-US", "es", "et", "fi", "fr", "hu", "id", "it", "ja", "ko", "lt", "lv", "nb", "nl", "pl", "pt-PT", "pt-BR", "ro", "ru", "sk", "sl", "sv", "tr", "uk", "zh"];
const langListSonaveeb = ["et"];
const langListSonaveeb = ["en", "et", "fr", "ru", "uk"];
export default (translationApi) => {
const langList = translationApi === "google"

View file

@ -7,6 +7,14 @@ let translationHistory = [];
const logDir = "common/translate";
const iso639Map = {
'en': 'eng',
'et': 'est',
'fr': 'fra',
'ru': 'rus',
'uk': 'ukr'
};
const getHistory = (sourceWord, sourceLang, targetLang, translationApi) => {
const history = translationHistory.find(
history =>
@ -109,7 +117,7 @@ const sendRequestToDeepL = async (word, sourceLang, targetLang) => {
return resultData;
};
const sendRequestToSonaveeb = async (word) => {
const sendRequestToSonaveeb = async (word, lang = "est") => {
const resultData = {
resultText: "",
rawHTML: true,
@ -121,14 +129,36 @@ const sendRequestToSonaveeb = async (word) => {
};
const domParser = new DOMParser();
const mainPage = await _sendRequestToSonaveeb(`https://sonaveeb.ee/search/unif/dlall/dsall/${word}/1`, resultData);
const mainPage = await _sendRequestToSonaveeb(`https://sonaveeb.ee/search/unif/dlall/dsall/${word}/1/${lang}`, resultData);
if (resultData.isError) {
return resultData;
}
const mainDocument = domParser.parseFromString(mainPage.data, "text/html");
const wordIdElement = mainDocument.querySelector("input[name=\"word-id\"]");
if (wordIdElement) {
const wordIdElements = mainDocument.querySelectorAll("input[name=\"word-id\"]");
if (wordIdElements.length > 0) {
let wordIdElement;
if (wordIdElements.length === 1) {
wordIdElement = wordIdElements[0];
} else {
wordIdElements.forEach(el => {
if (!wordIdElement) {
const urlElement = el.parentNode.querySelector("input[name=\"word-select-url\"]");
if (urlElement) {
const urlMatch = urlElement.value.match("/([^/]+)$");
if (urlMatch && urlMatch.length >= 1) {
const urlLang = urlMatch[1];
if (urlLang === lang) {
wordIdElement = el;
}
}
}
}
});
if (!wordIdElement) { // safety fallback
wordIdElement = wordIdElements[0];
}
}
const wordId = wordIdElement.getAttribute("value");
const wordDetailsPage = await _sendRequestToSonaveeb(`https://sonaveeb.ee/worddetails/unif/${wordId}`, resultData);
@ -143,7 +173,7 @@ const sendRequestToSonaveeb = async (word) => {
if (morphoModalElement) {
const paradigmId = morphoModalElement.getAttribute("data-paradigm-id");
const morphoPage = await _sendRequestToSonaveeb(`https://sonaveeb.ee/morpho/unif/${paradigmId}/est`, resultData);
const morphoPage = await _sendRequestToSonaveeb(`https://sonaveeb.ee/morpho/unif/${paradigmId}/${lang}`, resultData);
if (!resultData.isError) {
resultData.resultText += morphoPage.data;
} else {
@ -204,7 +234,7 @@ export default async (sourceWord, sourceLang = "auto", targetLang, translationAp
? await sendRequestToGoogle(sourceWord, sourceLang, targetLang)
: (translationApi === "deepl"
? await sendRequestToDeepL(sourceWord, sourceLang, targetLang)
: await sendRequestToSonaveeb(sourceWord)
: await sendRequestToSonaveeb(sourceWord, iso639Map[targetLang] || targetLang)
);
setHistory(sourceWord, sourceLang, targetLang, translationApi, result);
return result;

View file

@ -1,6 +1,6 @@
{
"manifest_version": 2,
"version": "2.8.3",
"version": "2.8.4",
"name": "__MSG_extName__",
"description": "__MSG_extDescription__",
"default_locale": "en",