A simple script to use DeepL to do translations.

It will ask you for a token the DeepL and from which language to which language you want to translate. I have added the function to choose between different translations but as far as I can see it is always returning only one,

link to install 🔥

// Name: Translate with DeepL
// Author: Jon E. Eguiluz
// Twitter: @viroide
import "@johnlindquist/kit"
let DEEPL_API_KEY = await env("DEEPL_API_KEY", {
hint: md(
`Get a [DEEPL_API_KEY API Key](https://www.deepl.com/account/summary)`
),
ignoreBlur: true,
secret: true,
});
let SOURCE_LANG = await env("SOURCE_LANG", {
hint: md(
`What language do you write in? (EN, ES... [Full list](https://www.deepl.com/es/docs-api/translating-text/request/))`
),
});
let TARGET_LANG = await env("TARGET_LANG", {
hint: md(
`What language do you want to translate into? (EN, ES... [Full list](https://www.deepl.com/es/docs-api/translating-text/request/))`
),
});
const query = (input) =>
`https://api-free.deepl.com/v2/translate?text=${encodeURIComponent(input)}&source_lang=${SOURCE_LANG}&target_lang=${TARGET_LANG}&auth_key=${DEEPL_API_KEY}`;
let lastTimestamp = null;
let selectedTranslation = await arg(
"Translate with DeepL",
async input => {
// input check
if (!input || input.length < 4) return []
// delay check
if (lastTimestamp && Date.now() - lastTimestamp < 200) return []
lastTimestamp = Date.now()
const query_url = query(input);
let { data } = await get(query_url);
return data.translations.map(translation => {
return {
name: translation.text,
preview: md(`${translation.text}`),
value: translation.text
}
})
}
);
await setSelectedText(selectedTranslation);