added deepl translations

This commit is contained in:
Hazel Noack 2025-07-11 14:34:39 +02:00
parent ad204ee4e5
commit 89d351c6e3

View File

@ -26,6 +26,22 @@ const searchEngines = {
}, },
}; };
const translationPrefixes = [
"t",
"translation",
]
function getDeepLUrl(s) {
const parts = s.split("-")
if (parts.length != 3) {
return undefined
}
return `https://www.deepl.com/en/translator?/#${encodeURIComponent(parts[0].trim())}/${encodeURIComponent(parts[1].trim())}/${encodeURIComponent(parts[2].trim())}`;
}
form.addEventListener("submit", event => { form.addEventListener("submit", event => {
event.preventDefault(); event.preventDefault();
@ -37,6 +53,25 @@ form.addEventListener("submit", event => {
return return
} }
// deepl translations
let doTranslation = false;
for (const value of translationPrefixes) {
const prefix = `!${value} `;
if (s.startsWith(prefix)) {
doTranslation = true;
s = s.slice(prefix.length); // Remove the !{key} prefix
break;
}
}
if (doTranslation) {
const url = getDeepLUrl(s);
if (url) {
window.open(url.toString(), "_self");
return;
}
}
// Check if the string starts with ! followed by a key from searchEngines // Check if the string starts with ! followed by a key from searchEngines
let selectedEngine = { let selectedEngine = {
action: form.getAttribute("action"), action: form.getAttribute("action"),