Back to blog
Salesforce, Integration

How to integrate Salesforce with Google translate.

Integration Salesforce with Google Translate

In this article, we will show you how to integrate Salesforce with Google Translate. You will learn how to translate text in Salesforce using a Lightning Web Component and the Google Translation API.

Here’s a sample of what we will be developing.

You might think of looking for a package on the AppExchange, but it is highly probable they use Google Translation API as well, and it is actually really easy to use.

“I have enjoyed increasing my potential customers, and I hope to reach more of them.”

SPENCER LÓPEZ - MARKETING

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.

Nadia Recarey

More Notes

How Modelit’s Talent Team delivers top Salesforce staff

Learn how Modelit’s Talent Team quickly matches you with top Salesforce talent through a personalized and painless hiring process.

Agostina Cataldi
Read more
arrow

Modelit’s top takeaways from Dreamforce 2024

Curious about the biggest news out of Dreamforce? Here’s how Agentforce AI and Data Cloud integration are set to transform customer service!

The Modelit Team
Read more
arrow

Dreamforce’s Main Keynote: Are you ready for Agentforce?

Catch our highlights from Dreamforce 2024’s Main Keynote! Find out how Salesforce is redefining customer service with it’s new Agentforce.

The Modelit Team
Read more
arrow