How to integrate Salesforce with Google translate.

How to integrate Salesforce with Google translate.
Salesforce, Integration
Google APIs, Google translation API, Salesforce, Salesforce lightning, Integration
About the author
Nadia Recarey
Salesforce specialist
This placeholder text could be a few words about the author and their experience with Salesforce and/or Modelit.
Share this article

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.

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.

About the author
Nadia Recarey
Salesforce specialist
This placeholder text could be a few words about the author and their experience with Salesforce and/or Modelit.
Share this article
How to integrate Salesforce with Google translate.
How to integrate Salesforce with Google translate.
Re-writing your career path: how to start a job in IT.
Re-writing your career path: how to start a job in IT.
Salesforce Summer ’21 Release is here, we picked up the top 5 new features!
Salesforce Summer ’21 Release is here, we picked up the top 5 new features!
A day to learn, connect and have fun.
A day to learn, connect and have fun.
Welcome to our new website
Welcome to our new website
Previous
There are no previous blog posts.
Next
There are no next blog posts.
No comments yet. Be the first!

Leave a reply

Your email address will not be published. Required fields are marked *
Thank you! Your reply has been received!
Oops! Something went wrong while posting the comment.