Created
December 5, 2019 01:02
-
-
Save grayside/6999838f742e3c260e2577715a542126 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
FROM node:10-slim | |
WORKDIR /usr/src/app | |
COPY package*.json ./ | |
RUN npm install --only=production | |
COPY . ./ | |
CMD [ "npm", "start" ] |
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').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