Skip to content

Instantly share code, notes, and snippets.

@grayside
Created December 5, 2019 01:02
Show Gist options
  • Save grayside/6999838f742e3c260e2577715a542126 to your computer and use it in GitHub Desktop.
Save grayside/6999838f742e3c260e2577715a542126 to your computer and use it in GitHub Desktop.
Hello Translate
FROM node:10-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . ./
CMD [ "npm", "start" ]
const {TranslationServiceClient} = require('@google-cloud/translate').v3;
const translationClient = new TranslationServiceClient();
exports.helloTranslate = async (req, res) => {
try {
const projectId = await translationClient.getProjectId()
const request = {
parent: `projects/${projectId}`,
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