Created
May 24, 2020 12:33
-
-
Save securetorobert/58fa8ca11d08e9ad8f32f021a667dcef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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