// GraalJS Script (engine type: 2)
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthKey = configs.get ( "StrConfU" ); /// REQUIRED
const strSourceLang = configs.get ( "StrConfA1" ); // NotRequired
if( strSourceLang === "" ){
engine.log( " AutomatedTask ConfigWarning:" +
" Config {A1:SourceLang} is empty" );
}else{
engine.log( " AutomatedTask Config: SourceLang: " + strSourceLang );
}
const strSourceText = configs.get ( "StrConfA2" ); /// REQUIRED
if( strSourceText === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {A2:SourceText} is empty \n" );
}
let strSourceFormat = "text";
let strSourceFormatTmp = configs.get ( "StrConfA3" ); // NotRequired
if( strSourceFormatTmp === "xml" ){
strSourceFormat = "xml";
}else if( strSourceFormatTmp === "html" ){ // regarded as "xml" for versatility
strSourceFormat = "xml";
}
const strTargetLang = configs.get ( "StrConfB1" ); /// REQUIRED
if( strTargetLang === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {B1:TargetLang} is empty \n" );
}
const strPocketTargetText = configs.getObject( "SelectConfB2" ); /// REQUIRED
let strFormality = configs.get ( "StrConfB3" ); // NotRequired
//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)
//// == Calculating / 演算 ==
/// (DeepL > Translating text)
/// https://www.deepl.com/docs-api/translating-text/request/
// request1, prepare
let postUri1 = "https://api.deepl.com/v2/translate";
let request1 = httpClient.begin(); // HttpRequestWrapper
request1 = request1.queryParam( "auth_key", strAuthKey );
request1 = request1.queryParam( "text", strSourceText );
if( strSourceFormat === "xml" ){
request1 = request1.queryParam( "tag_handling", strSourceFormat );
}
if( strSourceLang !== "" ){
request1 = request1.queryParam( "source_lang", strSourceLang );
}
request1 = request1.queryParam( "target_lang", strTargetLang );
// request1, try
const response1 = request1.post( postUri1 ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + postUri1 );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
response1Code + "\n" + response1Body + "\n" );
}
// response1, parse
engine.log( response1Body ); // debug
/*
{
"translations": [
{
"detected_source_language":"EN",
"text":"私は猫です。まだ、名前はありません。どこで生まれたのかもわかりません。"
}
]
}
*/
const response1Obj = JSON.parse( response1Body );
let strTargetLangDetected = response1Obj.translations[0].detected_source_language;
engine.log( " AutomatedTask ApiResponce1: Detected Lang: " + strTargetLangDetected );
let strTargetText = response1Obj.translations[0].text;
//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( strPocketTargetText, strTargetText );
} //////// END "main()" /////////////////////////////////////////////////////////////////
/*
Notes:
- Translates String in the workflow. Machine translation is performed using DeepL API v2.
- Charged for Cloud Translation based on monthly usage.
- https://www.deepl.com/pro#developer
- The input text can be plain text or XML. (any XML tags in the input not be translated)
- https://www.deepl.com/docs-api/handling-xml/
- You need an authentication key to access to the API.
- Account Settings: https://www.deepl.com/pro-account/plan
- The request size should not exceed 30k bytes.
Notes-ja:
- ワークフローに格納されている「文字列」を自動翻訳します。DeepL API v2 で機械翻訳されます。
- 月間使用量に基づいて料金が請求されます。
- https://www.deepl.com/pro#developer
- プレーンテキストまたはXMLフォーマットの文字列を翻訳します。(XMLタグは翻訳されません)
- https://www.deepl.com/docs-api/handling-xml/
- APIにアクセスするための認証キーが必要です。
- Account Settings: https://www.deepl.com/pro-account/plan
- リクエストの上限は 30k bytes です。
*/
/*
APPENDIX
- Supported Language Codes in DeepL API (v2) - Can be specified in lowercase
- Language of the text to be translated (Source Lang)
- "DE" - German
- "EN" - English
- "FR" - French
- "IT" - Italian
- "JA" - Japanese
- "ES" - Spanish
- "NL" - Dutch
- "PL" - Polish
- "PT" - Portuguese (all Portuguese varieties mixed)
- "RU" - Russian
- "ZH" - Chinese
- The language into which the text should be translated. (Target Lang)
- "DE" - German
- "EN-GB" - English (British)
- "EN-US" - English (American)
- "EN" - English (for backward compatibility; please select EN-GB or EN-US instead)
- "FR" - French
- "IT" - Italian
- "JA" - Japanese
- "ES" - Spanish
- "NL" - Dutch
- "PL" - Polish
- "PT-PT" - Portuguese (all Portuguese varieties excluding Brazilian Portuguese)
- "PT-BR" - Portuguese (Brazilian)
- "PT" - Portuguese (for backward compatibility; please select PT-PT or PT-BR instead)
- "RU" - Russian
- "ZH" - Chinese
- https://www.deepl.com/docs-api/translating-text/request/
- Uncommon character sequences, which may be recognized markers within your system
- Might get translated or removed, resulting in a corrupted structure.
- Convert your markers to XML tags and enable XML handling.
- If "html" is set in Source Text Format, it is assumed that "xml" is set.
- Error Example
- AutomatedTask ApiResponse Status: 400
- AutomatedTask UnexpectedResponseError: 400
- {"message":"Value for 'target_lang' not supported."}
APPENDIX-ja
- DeepL API (v2) でサポートされている言語コード(小文字指定も可)
- 翻訳元言語(ソース言語)
- "DE" - German
- "EN" - English
- "FR" - French
- "IT" - Italian
- "JA" - Japanese
- "ES" - Spanish
- "NL" - Dutch
- "PL" - Polish
- "PT" - Portuguese (全ポルトガル語)
- "RU" - Russian
- "ZH" - Chinese
- 翻訳先言語(ターゲット言語)
- "DE" - German
- "EN-GB" - English (British)
- "EN-US" - English (American)
- "EN" - English (下位互換のためのコード。推奨は EN-GB もしくは EN-US)
- "FR" - French
- "IT" - Italian
- "JA" - Japanese
- "ES" - Spanish
- "NL" - Dutch
- "PL" - Polish
- "PT-PT" - Portuguese (ブラジルを除くポルトガル語)
- "PT-BR" - Portuguese (ブラジルのポルトガル語)
- "PT" - Portuguese (下位互換のためのコード。推奨は PT-PT もしくは PT-BR)
- "RU" - Russian
- "ZH" - Chinese
- https://www.deepl.com/docs-api/translating-text/request/
- システム利用想定のマーカー文字等
- 翻訳または削除され、構造が破損する可能性があります。
- 必要に応じて、マーカーをXMLタグに変換し、XML処理を有効にします。
- ソースフォーマットに "html" がセットされた場合、"xml" と読み替えます。
- Error Example
- AutomatedTask ApiResponse Status: 400
- AutomatedTask UnexpectedResponseError: 400
- {"message":"Value for 'target_lang' not supported."}
*/
Pingback: Google Translate: Translation API Basic, Translate – Questetra Support