Skip to content

Instantly share code, notes, and snippets.

@Richienb
Last active July 28, 2019 06:58
Show Gist options
  • Select an option

  • Save Richienb/1269f654756b7f93e1bc0c1b222c0ca5 to your computer and use it in GitHub Desktop.

Select an option

Save Richienb/1269f654756b7f93e1bc0c1b222c0ca5 to your computer and use it in GitHub Desktop.
Google Translate API
const sourceLang = "en"
const targetLang = "id"
const sourceText = "Hello"
const translate = (sourceLang, targetLang, sourceText) => new Promise((resolve, reject) => {
$.getJSON(`https://translate.googleapis.com/translate_a/single`, {
client: "gtx",
sl: sourceLang,
tl: targetLang,
dt: "t",
q: encodeURI(sourceText)
}).promise().then(data => resolve(data[0][0][0]))
})
translate("en", "id", "Hello").then(console.log) // "Halo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment