-
-
Save karim23657/40af1a995930f8e5cc95d8800fa30994 to your computer and use it in GitHub Desktop.
Gasでgoogle translateを自動言語検知ありで使えるやつです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function translate(text, from = "auto", to) { | |
try { | |
const result = UrlFetchApp.fetch( | |
"https://translate.google.com/_/TranslateWebserverUi/data/batchexecute", | |
{ | |
payload: | |
"f.req=" + | |
encodeURIComponent( | |
JSON.stringify([ | |
[ | |
[ | |
"MkEWBc", | |
`[[\"${text}\",\"${from}\",\"${to}\",true],[null]]`, | |
null, | |
"generic", | |
], | |
], | |
]) | |
), | |
} | |
).getContentText(); | |
return JSON.parse( | |
JSON.parse(result.split("\n").slice(2)[0])[0][2] | |
)[1][0][0][5][0][0]; | |
} catch { | |
return ""; | |
} | |
} | |
function createResponse(payload) { | |
return ContentService.createTextOutput() | |
.setMimeType(ContentService.MimeType.JSON) | |
.setContent(payload); | |
} | |
function doGet(event) { | |
const { text, from, to } = event.parameter; | |
if (!text || !to) return createResponse(""); | |
const result = translate(text, from, to); | |
return createResponse(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment