Skip to content

Instantly share code, notes, and snippets.

@grayside
Created September 27, 2019 18:22
Show Gist options
  • Save grayside/ed321053e41879b0277aeb70c7f654e0 to your computer and use it in GitHub Desktop.
Save grayside/ed321053e41879b0277aeb70c7f654e0 to your computer and use it in GitHub Desktop.
Hello Translate!
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