Created
September 27, 2019 18:22
-
-
Save grayside/ed321053e41879b0277aeb70c7f654e0 to your computer and use it in GitHub Desktop.
Hello Translate!
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 {TranslationServiceClient} = require('@google-cloud/translate').v3beta1; | |
const translationClient = new TranslationServiceClient(); | |
exports.helloTranslate = async (req, res) => { | |
try { | |
const request = { | |
parent: translationClient.locationPath(process.env.TRANSLATE_GCP_PROJECT, process.env.TRANSLATE_LOCATION), | |
contents: ['Hello World!'], | |
mimeType: 'text/plain', | |
sourceLanguageCode: 'en-US', | |
targetLanguageCode: req.query.lang || 'es', | |
}; | |
const [response] = await translationClient.translateText(request); | |
let message = response.translations[0].translatedText; | |
res.send(message); | |
} catch(err) { | |
console.error(err); | |
res.status(500).send(`Internal Service Error: ${err}`); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment