Skip to content

Instantly share code, notes, and snippets.

@Explosion-Scratch
Created September 11, 2021 02:30
Show Gist options
  • Save Explosion-Scratch/718cb436e288126fa4399b8fcf23e9ba to your computer and use it in GitHub Desktop.
Save Explosion-Scratch/718cb436e288126fa4399b8fcf23e9ba to your computer and use it in GitHub Desktop.
Translate text using the google translate API
// const {translated} = await translate("bonjour");
// --> "Hello"
async function translate(text, target, source) {
var opts = {
text: text || "",
source: source || 'auto',
target: target || "en",
}
var result = await fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${opts.source}&tl=${opts.target}&dt=t&q=${encodeURI(opts.text)}`).then(res => res.json());
return {
source: opts.source,
translated: result[0][0][0],
result,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment