3
2
2023-08-07
(C) Questetra, Inc. (MIT License)
https://support.questetra.com/bpmn-icons/service-task-deepl-text-translate/
https://support.questetra.com/ja/bpmn-icons/service-task-deepl-text-translate/
This item translates text using DeepL API v2.
It is also possible to translate into more formal or broken sentences (Formality).
この工程は、DeepL API(v2)を用いて機械翻訳します。フォーマルな文章や砕けた文章に翻訳することも可能です。
-
-
-
-
-
-
-
-
-
-
-
-
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADwklEQVRYR8WXe0hTURzHv8dHTrdm
9sBIKKH+qEll+aisDDVXVBYoab4qX+GjUisweksGBVJWUoHZm8yw/tCo2BKLigIrKdgMShAps4fa
VtZ8ntjd7trmvXdXKXb/Gju/3+98zvk9D4GTPyJ2/ylBMV4/fxtiCUgEJXQeQPwB+Jj1uwHaSihp
oqANMk/J7faXdb/E2HYIIFWsmuxKBooAZAOQiDEKwADg3CB1O9ajvdshpCMI4K2IzqaElAKQitzY
XqyHULpLp1Wf49PnBZAHKM8AyBnlxvZqZ/UaVS6XLU4AeUD0dYAk/qPNzWZolV6jTrK3OQxA7Mk3
xq5ESuwKfOvS4URlNRpfN4vhHXYTNgBmn58VsrQkZA6KspMRFjwbbq6ujKj+Zw+q6+px/Hw12j9/
EwQhlOZYx4QFwBzt7/kCborvRJTsysLqyDBIPMZwbvK16zvKL99CWeVNIYieQeo2g80OC4A8QHkC
QAGXZkFGPLZuisOk8eMcXjOlFG+aW1B88iLqn77gky/Ta1SFxkUGgCkyht5Orjwv3ZuH9IQ1cHVx
YYwZevugfdeKaX6+mODjzQvUrfuBnSXluHXvIZeMQSbxmGAsVgyAXBGdAkKucknWnj+KZQvnWZbu
NjxD4rZDKMpJxs6sRHiMceeE6OsfQFllNY6UX+GGpDRVr1VfYwC8FcpKSpAuBuBtSxvyD5VhrMwL
K8JDkRATBblseJ1yBEAoLui0qgzTDQREvwLI32NakdjfALtk9HXrhw5UVNUifEEg87dihj+m+vky
vx0BALRJr1HPNwMou6wai81F8AGwQoNDQ7jz4Ck27ijBlqS1KC7MgJenRAQAuvUa1XgWgPJFkxCA
MSBb2j5CLpXi9OUaRIYFQbk0FC4uRAwA9BoVGTGAMbobnr2Cu7sb1I8bkRG/BnNmTQcxmbJ8jl0A
GwDRLqhVP0Fq4WGmLuzJS4XUy3N0WQAbF/AHoX0d+PSlExVVdVCGh2BBoGLYyVkaB3UANkEolIYy
qScObE9D4rrlnOlmf3w2O45X3MCV2/d5C5VtGgoUItbCzOlTUZSTglURi3h7QWe3Dqcu1TjqBSaT
1oVIqBTbHyFqcTAO5qdxBt6j501Ym7mb99RWC7al2FSM+JsRl8Vtm+OQuSEG0/wmW+JgBAC2zci4
gaN2zAVhHx8iAbjbsaknMEOo4EDCBRIydxb2bd2Evv5+rM/dL+gC3oGE1RI7kolxNIeM8Ej2F8KJ
Q+l/uomRjeUshFMfJiyEU59m1kHktMfpKKNdtNofFmLMMLzOuicAAAAASUVORK5CYII=
{
const authSetting = httpClient.createAuthSettingToken('DeepL API', authKey);
configs.putObject('conf_Auth', authSetting);
configs.put('conf_SourceLangCode', sourceLangCode);
configs.put('conf_SourceText', sourceText);
configs.put('conf_SourceFormat', sourceFormat);
configs.put('conf_TargetLangCode', targetLangCode);
configs.put('conf_Formality', formality);
const translatedDef = engine.createDataDefinition('翻訳後のテキスト', 1, 'q_Translated', 'STRING_TEXTAREA');
engine.setData(translatedDef, '事前文字列');
configs.putObject('conf_TranslatedDef', translatedDef);
return translatedDef;
};
/**
* 異常系のテスト
* @param func
* @param errorMsg
*/
const assertError = (func, errorMsg) => {
try {
func();
fail();
} catch (e) {
expect(e.toString()).toEqual(errorMsg);
}
};
/**
* 翻訳元ソーステキストが空
*/
test('Source Text is empty', () => {
prepareConfigs(AUTH_KEY_PRO, 'EN', '', 'text', 'JA', '');
assertError(main, 'Source Text is empty.');
});
/**
* テキスト翻訳の API リクエストのテスト
* @param {Object} request
* @param request.url
* @param request.method
* @param request.contentType
* @param request.body
* @param {String} expectedUrl
* @param {String} authKey
* @param {String} sourceLangCode
* @param {String} sourceText
* @param {String} tagHandling
* @param {String} targetLangCode
* @param {String} formality
*/
const assertRequest = ({url, method, contentType, body}, expectedUrl, authKey, sourceLangCode, sourceText, tagHandling, targetLangCode, formality) => {
expect(url).toEqual(`${expectedUrl}?auth_key=${encodeURIComponent(authKey)}`);
expect(method).toEqual('POST');
expect(contentType).toEqual('application/json');
const bodyObj = JSON.parse(body);
if (sourceLangCode !== null) {
expect(bodyObj.source_lang).toEqual(sourceLangCode);
}
expect(bodyObj.text[0]).toEqual(sourceText);
if (tagHandling !== null) {
expect(bodyObj.tag_handling).toEqual(tagHandling);
}
expect(bodyObj.target_lang).toEqual(targetLangCode);
if (formality !== null) {
expect(bodyObj.formality).toEqual(formality);
}
};
/**
* テキスト翻訳の API リクエストでエラー
*/
test('Fail to translate', () => {
prepareConfigs(AUTH_KEY_PRO, 'EN', 'Hello.', 'text', 'JA', '');
httpClient.setRequestHandler((request) => {
assertRequest(request, API_URL_PRO, AUTH_KEY_PRO, 'EN', 'Hello.', null, 'JA', null);
return httpClient.createHttpResponse(400, 'application/json', '{}');
});
assertError(main, 'Failed to translate. status: 400');
});
/**
* テキスト翻訳 API のレスポンスボディを作成
* @param {String} detectedSourceLangCode
* @param {String} translatedText
* @returns {String} response
*/
const createResponse = (detectedSourceLangCode, translatedText) => {
const responseObj = {
"translations": [
{
"detected_source_language": detectedSourceLangCode,
"text": translatedText
}
]
};
return JSON.stringify(responseObj);
};
/**
* 成功
* Pro アカウント
* 英語から日本語(翻訳元言語コードを指定)
* プレーンテキスト
* formality 指定なし
*/
test('Success - pro account, with explicit source lang code, text, default formality', () => {
const sourceText = 'Hello.';
const translatedDef = prepareConfigs(AUTH_KEY_PRO, 'EN', sourceText, 'text', 'JA', '');
const translatedText = 'こんにちは。';
httpClient.setRequestHandler((request) => {
assertRequest(request, API_URL_PRO, AUTH_KEY_PRO, 'EN', sourceText, null, 'JA', null);
return httpClient.createHttpResponse(200, 'application/json', createResponse('EN', translatedText));
});
expect(main()).toEqual(undefined);
// データ項目の値をチェック
expect(engine.findData(translatedDef)).toEqual(translatedText);
});
/**
* 成功
* Pro アカウント
* 英語から日本語(翻訳元言語コードの指定なし)
* xml
* formality に prefer_more を指定
*/
test('Success - pro account, without source lang code, xml, default formality', () => {
const sourceText = 'Hello';
const translatedDef = prepareConfigs(AUTH_KEY_PRO, '', sourceText, 'xml', 'JA', 'prefer_more');
const translatedText = 'こんにちは';
httpClient.setRequestHandler((request) => {
assertRequest(request, API_URL_PRO, AUTH_KEY_PRO, null, sourceText, 'xml', 'JA', 'prefer_more');
return httpClient.createHttpResponse(200, 'application/json', createResponse('EN', translatedText));
});
expect(main()).toEqual(undefined);
// データ項目の値をチェック
expect(engine.findData(translatedDef)).toEqual(translatedText);
});
/**
* 成功
* Free アカウント
* 日本語からイギリス英語
* html
* formality に prefer_less を指定
*/
test('Success - pro account, with explicit source lang code, html, with preferred formality option', () => {
const sourceText = 'こんにちは';
const translatedDef = prepareConfigs(AUTH_KEY_FREE, 'JA', sourceText, 'html', 'EN-GB', 'prefer_less');
const translatedText = 'Hello';
httpClient.setRequestHandler((request) => {
assertRequest(request, API_URL_FREE, AUTH_KEY_FREE, 'JA', sourceText, 'html', 'EN-GB', 'prefer_less');
return httpClient.createHttpResponse(200, 'application/json', createResponse('JA', translatedText));
});
expect(main()).toEqual(undefined);
// データ項目の値をチェック
expect(engine.findData(translatedDef)).toEqual(translatedText);
});
]]>