Last active
January 9, 2020 09:36
-
-
Save daliborgogic/60f346a506cf9593d6d5f37880c3c8e6 to your computer and use it in GitHub Desktop.
Cloud Translation v2
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
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