Fraud Blocker
No items found.
No items found.

How to integrate Salesforce with Google translate.

You can check out the details here

https://cloud.google.com/translate and prices here https://cloud.google.com/translate/
pricing. Bear in mind that translation usage is calculated in millions of characters, where 1 million = 106 characters.

It might be helpful if you already have some knowledge about:

  1. Apex and Lightning Web Component development
  2. API requests/responses

What do we need?

A project on Google Cloud to use the Translate API (we will use the API Key that Google provides us). For more information about how to create a project on Google Cloud, click here.

What will be developing?

We will create a simple Lightning Web Component that will only contain an input field where we are going to enter our text that we want to translate, and a button that will call our apex method to translate it.

We’ll create:
  • An Apex class

GoogleTranslateHelper: A Google Translate Helper class that will contain the callout to the Google Translate API.

  • A Lightning Web Component

Translate: A simple LWC that will show us the translated text.

You can see the code in the following GitHub repo

https://github.com/ NadiaRecarey/ googleTranslationOnSalesforce .

Ok, let’s dive into it!

1. Store your API Key

When you get your API Key from Google, you might want to have it stored somewhere in Salesforce rather than hardcoded, so we are going to create a new field called “Google_Translate_API_Key__c” on a custom setting to save it there.

2. Register Google Translation API endpoint in the Remote Site Settings page

To add a remote site setting:

  1. From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
  2. Click New Remote Site.
  3. Enter Google_Translate as Remote Site Name.
  4. Enter https://www.googleapis.com as the URL for the remote site.
  5. Optionally, enter a description of the site.
  6. Click Save.

3. Create an Apex Class: GoogleTranslateHelper

This class will contain two methods: getTranslatedText and googleTranslateCallout.

Method googleTranslateCallout

As we see on the comment before the method declaration, the response we should expect from the Google Translation API is a JSON that contains an array within the data object. This array, Translations, will contain all the translated texts we sent in the request, and the detected language of the original text.

This method will be called directly from the LWC and will receive a list of texts we wish to translate. Remember that for us to be able to call Apex methods from LWC js files, we have to denote the method with the @auraEnabled decorator.

In line 37 we call the other method googleTranslateCallout, which will actually make the callout to the google API.

After we got a response from the callout, we parse it using the JSON Apex class and then return it.

As we see on the comment before the method declaration, the response we should expect from the Google Translation API is a JSON that contains an array within the data object. This array, Translations, will contain all the translated texts we sent in the request, and the detected language of the original text.

This method will be called directly from the LWC and will receive a list of texts we wish to translate. Remember that for us to be able to call Apex methods from LWC js files, we have to denote the method with the @auraEnabled decorator.

In line 37 we call the other method googleTranslateCallout, which will actually make the callout to the google API.

After we got a response from the callout, we parse it using the JSON Apex class and then return it.