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 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 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) => { export default (translationApi) => {
const langList = translationApi === "google" const langList = translationApi === "google"

View file

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

View file

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