Updating the controller

Now that are done with defining the routes and updating the model, we will work on the logic discussed in the Solution design section earlier. We are going to add two new methods to CloudAIAPI, named getSupportedLanguages and translateMessage. Open server\controllers\cloud-ai-api.ts and we will first add the required imports and LanguageServiceClient. Before the class definition, add this code:

// SNIPP SNIPPconst Translate = require('@google-cloud/translate');const translateClient = new Translate({    credentials: JSON.parse(process.env.GCP_SK_CREDENTIALS)});// SNIPP SNIPP

Using the environment variable we have set in the .env file, we are using GCP_SK_CREDENTIALS value to initialize a new Translate client. Next, we ...

Get Google Cloud AI Services Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.