Skip to content

Instantly share code, notes, and snippets.

@karim23657
Forked from Googlefan256/gas.js
Created February 8, 2024 06:58
Show Gist options
  • Save karim23657/40af1a995930f8e5cc95d8800fa30994 to your computer and use it in GitHub Desktop.
Save karim23657/40af1a995930f8e5cc95d8800fa30994 to your computer and use it in GitHub Desktop.
Gasでgoogle translateを自動言語検知ありで使えるやつです
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