Created
March 26, 2025 14:33
-
-
Save JoepKockelkorn/6c283ecae74413062a749a10cbe6591c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shortcut: | |
// Name: Translate with DeepL | |
// Description: Translate a text from a lang to a lang | |
// 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 arg({ | |
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 arg({ | |
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