Create a Google Cloud Translation REST API Service
In this particular post, you will learn to create and use the Google cloud translation REST API service. It is a pre-trained model over 100 languages to translate the data. Translation API helps web and mobile applications contents to translate content from one language to another language.
Pre-requisites
- Set-up google cloud account
Step 1: Enable Cloud Translation API
Enable the cloud translation API in the google developer console.
Step 2: Create a Google Service Account to access API
In the google cloud console, search for IAM & Admin and click on the service accounts. Create a Google Service Account with Cloud Translation API Editor role.
Generate the Key.json for the for the generated google service account
Step 3: Access the Google Cloud shell to invoke Translation API
In the top-right corner, click in the cloud shell. Cloud shell is a google provided Linux playground environment.
Access the cloud shell. Upload the generated key.json and export the GOOGLE_APPLICATION_CREDENTIALS as shown below.
export GOOGLE_APPLICATION_CREDENTIALS='key.json'
Note: If it doesn’t work, then provide the complete file path.
Step 4: Test the Google Translation Service in Cloud Shell
Create the request.json file as follows. Google translation service automatically identifies the source language.
In the following example, you are converting the source text to the target language Hindi.
{"q": ["How are you feeling today"],"target": "hi"}
Use the curl command to hit the Translation API endpoint
$ curl -X POST -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" -d @request.json https://translation.googleapis.com/language/translate/v2
Output
You can test the same using postman as well.
Congratulations :) Now you successfully learned how to create and use the Google Cloud Translation API service.