Skip to content

Instantly share code, notes, and snippets.

@securetorobert
Created May 24, 2020 12:33
Show Gist options
  • Save securetorobert/58fa8ca11d08e9ad8f32f021a667dcef to your computer and use it in GitHub Desktop.
Save securetorobert/58fa8ca11d08e9ad8f32f021a667dcef to your computer and use it in GitHub Desktop.
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const text = 'The text to translate, e.g. Hello, world!';
// const target = 'The target language, e.g. ru';
async function translateText() {
// Translates the text into the target language. "text" can be a string for
// translating a single piece of text, or an array of strings for translating
// multiple texts.
let [translations] = await translate.translate(text, target);
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:');
translations.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
});
}
translateText();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment