YouTubeで解説しています → https://youtu.be/2lFDi_86Omo
プログラム
コード.gs
const deepl_url = "https://api-free.deepl.com/v2/translate";
const deepl_key = "DeepL-Auth-Key *************************************"
/**
curl -X POST 'https://api-free.deepl.com/v2/translate' \
-H 'Authorization: DeepL-Auth-Key **********************************:fx' \
-d 'text=Hello%2C%20world!' \
-d 'target_lang=DE'
*/
// 翻訳
function deepL(msg) {
// msg = "今日の天気は?"
msg = "What is the weather today"
let lang = 'EN'
if (isEnglish(msg))
lang = 'JA'
let headers = {
'Authorization' : deepl_key
}
let payload = {
'text' : msg,
'target_lang' : lang
}
let options = {
'method' : 'post',
'headers' : headers,
'payload' : payload
}
const response = UrlFetchApp.fetch(deepl_url, options).getContentText()
const json = JSON.parse(response)
const text = json.translations[0].text
return text
}
// 英語かどうか判定
function isEnglish(text) {
return !text.split('').some(char => char.charCodeAt() > 255);
}
参考リンク
◆ DeepL 公式ページ
https://www.deepl.com/
◆ DeepL API 料金表
https://www.deepl.com/pro?cta=header-prices
◆ DeepL API ドキュメント
https://www.deepl.com/docs-api/translate-text/