From e755656fcdccff63562346cc21a44cc190aa2c4a Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Thu, 21 Nov 2024 02:31:43 +0100 Subject: [PATCH] =?UTF-8?q?Multilanguage=20support=20for=20S=C3=B5naveeb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/generateLangOptions.js | 2 +- src/common/translate.js | 42 ++++++++++++++++++++++++++----- src/manifest-firefox.json | 2 +- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/common/generateLangOptions.js b/src/common/generateLangOptions.js index 03359d7..43fb862 100644 --- a/src/common/generateLangOptions.js +++ b/src/common/generateLangOptions.js @@ -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" diff --git a/src/common/translate.js b/src/common/translate.js index 1514b0c..d1e05be 100644 --- a/src/common/translate.js +++ b/src/common/translate.js @@ -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; diff --git a/src/manifest-firefox.json b/src/manifest-firefox.json index 5fc2fa8..3332cff 100644 --- a/src/manifest-firefox.json +++ b/src/manifest-firefox.json @@ -1,6 +1,6 @@ { "manifest_version": 2, - "version": "2.8.3", + "version": "2.8.4", "name": "__MSG_extName__", "description": "__MSG_extDescription__", "default_locale": "en",