Skip to content

Instantly share code, notes, and snippets.

@Googlefan256
Created September 3, 2022 13:53
Show Gist options
  • Save Googlefan256/f5267c93904f7e7ee18b79747e37d2a3 to your computer and use it in GitHub Desktop.
Save Googlefan256/f5267c93904f7e7ee18b79747e37d2a3 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