Skip to content

Instantly share code, notes, and snippets.

@Viroide
Created July 26, 2022 14:14
Show Gist options
  • Save Viroide/0b1c7653b49c7fd05831d3f306611a7b to your computer and use it in GitHub Desktop.
Save Viroide/0b1c7653b49c7fd05831d3f306611a7b to your computer and use it in GitHub Desktop.
// Shortcut: shift option control f9
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment