Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active January 9, 2020 09:36
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 daliborgogic/60f346a506cf9593d6d5f37880c3c8e6 to your computer and use it in GitHub Desktop.
Save daliborgogic/60f346a506cf9593d6d5f37880c3c8e6 to your computer and use it in GitHub Desktop.
Cloud Translation v2
const { Translate } = require('@google-cloud/translate').v2
const translate = new Translate()
const text = 'Hello, world!'
const target = 'da'
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]
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