DeepL: Translate Text

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_Auth
C1: Authorization Setting in which Authentication Key is set *
conf_SourceLangCode
C2: Source Lang Code (if blank, auto-detected)
conf_SourceText
C3: Source Text *#{EL}
conf_SourceFormat
C4: Source Text Format *
conf_TargetLangCode
C5: Target Lang Code *
conf_Formality
C6: Formality Option
conf_TranslatedDef
C7: Data item to save the translated text *

Notes

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • deepl-text-translate.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step


const API_URL_FREE = 'https://api-free.deepl.com/v2/translate';
const API_URL_PRO = 'https://api.deepl.com/v2/translate';

const main = () => {
    //// == Config Retrieving / 工程コンフィグの参照 ==
    const authSetting = configs.getObject('conf_Auth'); /// REQUIRED
    const sourceLangCode = configs.get('conf_SourceLangCode'); // NotRequired
    const sourceText = configs.get('conf_SourceText'); /// REQUIRED
    if (sourceText === '') {
        throw new Error('Source Text is empty.');
    }
    const sourceFormat = configs.get('conf_SourceFormat'); /// REQUIRED
    const targetLangCode = configs.get('conf_TargetLangCode'); /// REQUIRED
    const formality = configs.get('conf_Formality'); // NotRequired
    const translatedDef = configs.getObject('conf_TranslatedDef'); /// REQUIRED

    //// == Calculating / 演算 ==
    const translatedText = translate(authSetting, sourceLangCode, sourceText, sourceFormat, targetLangCode, formality);

    //// == Data Updating / ワークフローデータへの代入 ==
    engine.setData(translatedDef, translatedText);
};

/**
 * 翻訳する
 * @param {AuthSettingWrapper} authSetting
 * @param {String} sourceLangCode
 * @param {String} sourceText
 * @param {String} sourceFormat
 * @param {String} targetLangCode
 * @param {String} formality
 * @returns {String} translatedText
 */
const translate = (authSetting, sourceLangCode, sourceText, sourceFormat, targetLangCode, formality) => {
    /// (DeepL > Translating text)
    /// https://www.deepl.com/ja/docs-api/translate-text/
    const authKey = authSetting.getToken();
    const url = decideUrl(authKey);
    const body = buildBody(sourceLangCode, sourceText, sourceFormat, targetLangCode, formality);
    const response = httpClient.begin() // HttpRequestWrapper
        .authorization("DeepL-Auth-Key", authKey)
        .body(body, 'application/json')
        .post(url); // HttpResponseWrapper
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw new Error(`Failed to translate. status: ${status}`);
    }
    const responseObj = JSON.parse(responseStr);
    const detectedSourceLangCode = responseObj.translations[0].detected_source_language;
    engine.log(`Detected source language code: ${detectedSourceLangCode}`);
    const translatedText = responseObj.translations[0].text;
    return translatedText;
};

/**
 * API の URL を決定する
 * @param {String} authKey
 * @returns {String} url
 */
const decideUrl = (authKey) => {
    if (authKey.endsWith(':fx')) { // Free
        return API_URL_FREE;
    }
    // Pro
    return API_URL_PRO;
};

/**
 * API のリクエストボディを作成する
 * @param {String} sourceLangCode
 * @param {String} sourceText
 * @param {String} sourceFormat
 * @param {String} targetLangCode
 * @param {String} formality
 * @returns {String} body
 */
const buildBody = (sourceLangCode, sourceText, sourceFormat, targetLangCode, formality) => {
    const bodyObj = {
        text: [sourceText],
        target_lang: targetLangCode
    };
    if (sourceLangCode !== '') {
        Object.assign(bodyObj, { source_lang: sourceLangCode });
    }
    if (sourceFormat !== 'text') { // xml or html
        Object.assign(bodyObj, { tag_handling: sourceFormat });
    }
    if (formality !== '') {
        Object.assign(bodyObj, { formality });
    }
    return JSON.stringify(bodyObj);
};

    

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading