Action

DeepL Translate

Posted by FlohGro, Last update 9 months ago

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 - Bulgarian
  • CS - Czech
  • DA - Danish
  • DE - German
  • EL - Greek
  • EN - English (unspecified variant for backward compatibility; please select EN-GB or EN-USinstead)
  • EN-GB - English (British)
  • EN-US - English (American)
  • ES - Spanish
  • ET - Estonian
  • FI - Finnish
  • FR - French
  • HU - Hungarian
  • ID - Indonesian
  • IT - Italian
  • JA - Japanese
  • KO - Korean
  • LT - Lithuanian
  • LV - Latvian
  • NB - Norwegian (Bokmål)
  • NL - Dutch
  • PL - Polish
  • PT - Portuguese (unspecified variant for backward compatibility; please select PT-BR or PT-PT instead)
  • PT-BR - Portuguese (Brazilian)
  • PT-PT - Portuguese (all Portuguese varieties excluding Brazilian Portuguese)
  • RO - Romanian
  • RU - Russian
  • SK - Slovak
  • SL - Slovenian
  • SV - Swedish
  • TR - Turkish
  • UK - Ukrainian
  • ZH - 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

Buy Me A Coffee

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
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.