Skip to content

Instantly share code, notes, and snippets.

@jgwill
Last active April 11, 2019 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgwill/cc1d5610da640bb3fbffd6efefb24bb9 to your computer and use it in GitHub Desktop.
Save jgwill/cc1d5610da640bb3fbffd6efefb24bb9 to your computer and use it in GitHub Desktop.
Simple Translation using Google Translate API and NodeJS
//Install require an env var with your API keys
//export GOOGLE_APPLICATION_CREDENTIALS="/home/jgi/x/etc/mykeys-5oeu3uoeu.json"
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
// Instantiates a client
const translate = new Translate({
});
// The text to translate
const text = 'Hello, world! There is much more that just saying hello that is possible when you have a creative orientation.';
// The target language
const target = 'de';
// Translates some text into Russian
translate
.translate(text, target)
.then(results => {
const translation = results[0];
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
})
.catch(err => {
console.error('ERROR:', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment