Action
DeepL Translate
created by @FlohGro / more on my Blog
DeepL Translate
This Action uses the DeepL API to translate the text in the current draft to the specified target language. You can configure it to either append the translated text to the draft or replace the current content.
You need to register for a free account of the DeepL API to make use of this action. Copy the generated API Key and insert it into the Prompt for the API credential when you run this Action the first time.
[Configuration]
As mentioned, when you run this Action the first time, it will ask for an API Key of the DeepL API. Please copy and paste your key into that prompt. It will be stored in Drafts credentials - if you run into issues with the API you can „forget“ the credential in Drafts Settings
You need to set the target language (the language you want your draft to be translated to). By default this is EN
but you can change that to one of the options below. Please only insert the two letter language code into the „Define Template Tag“ step for target_language
Following options are available as target language:
BG
- BulgarianCS
- CzechDA
- DanishDE
- GermanEL
- GreekEN
- English (unspecified variant for backward compatibility; please selectEN-GB
orEN-US
instead)EN-GB
- English (British)EN-US
- English (American)ES
- SpanishET
- EstonianFI
- FinnishFR
- FrenchHU
- HungarianID
- IndonesianIT
- ItalianJA
- JapaneseKO
- KoreanLT
- LithuanianLV
- LatvianNB
- Norwegian (Bokmål)NL
- DutchPL
- PolishPT
- Portuguese (unspecified variant for backward compatibility; please selectPT-BR
orPT-PT
instead)PT-BR
- Portuguese (Brazilian)PT-PT
- Portuguese (all Portuguese varieties excluding Brazilian Portuguese)RO
- RomanianRU
- RussianSK
- SlovakSL
- SlovenianSV
- SwedishTR
- TurkishUK
- UkrainianZH
- Chinese (simplified)
You can always check an up-to-date list in the DeepL API reference
By default the Action will append the translation to the current draft. If you want the Action to replace the current content of the draft with the translated text you can change the Template tag result_insert_mode
to replace
. Please note: only append
and replace
are valid values for this template tag. If you use something else, it will default to appending the text
If you find this useful and want to support me you can donate or buy me a coffe
Steps
-
defineTemplateTag
name target_language
template EN
-
defineTemplateTag
name result_insert_mode
template append
-
script
let deeplCredential = new Credential("DeepL API Key","insert the API key from DeepL by registering on their API page") deeplCredential.addTextField("apiKey","DeepL API key") deeplCredential.authorize() let http = new HTTP(); // create HTTP object const textToTranslate = draft.content const targetLang = draft.processTemplate("[[target_language]]") let resultInsertMode = draft.processTemplate("[[result_insert_mode]]") // set default to append result if not set to "replace" in the template tag if(resultInsertMode != "replace"){ resultInsertMode = "append"; } let response = http.request({ "url": "https://api-free.deepl.com/v2/translate", "method": "POST", "headers": { "Authorization": "DeepL-Auth-Key " + deeplCredential.getValue("apiKey") }, "data": { "text": [textToTranslate], "target_lang": targetLang } }); if (response.success) { let data = response.responseData; let translatedText = "" for(translation of data.translations){ translatedText = translatedText + " " +translation.text translatedText = translatedText.trim() } if(resultInsertMode == "append"){ draft.append("\n---\n\n" + translatedText) } else { draft.content = translatedText } draft.update() } else { alert("Request to DeepL API failed!\n\nstatus code: " + response.statusCode + "\nresponse error:" + response.error + "\n\nplease ensure you have set a valid API key and configured the action according to the description (e.g. the target language).\nIf you still encounter issues reach out to FlohGro in the Drafts Forum") console.log(response.statusCode); console.log(response.error); }
Options
-
After Success Default Notification Info Log Level Info