Text translation is the process of converting any written text from one language to another. It involves taking a piece of text written in a source language and generating an equivalent piece of text in a target language while preserving the meaning and context as much as possible. This post will explain how to translate text in Salesforce using SYSTRAN API.
This is a continuation of our blog Low Code Integration for Text Translation using SysTran API which converts text in any language using Salesforce Flow. Salesforce flow is used to call external API using HTTP Callout. You can also check mentioned post to understand what is the use of text translation API in the Salesforce System. In this post, we will use Apex to call text translation SYSTRAN API and convert it into a different language.
Steps Required for Text Translation in Salesforce
- Get SYSTRAN API Access
- Create External and Named Credential
- Create a Permission set and Assign it to User
- Create a Custom Metadata Type for Languages
- Create an Apex class to convert text
- Test Text Translation using Lightning Web Component
Follow our blog Low Code Integration for Text Translation using SysTran API to complete the top 4 steps in detail. We will be focusing on using Apex and LWC for translating text in this post.
5. Create an Apex class to convert text
Create an apex class that integrates SYSTRAN API and converts source text into the desired language.
API Url :
https://api-translate.systran.net/translation/text/translate?key=36c46892-26c8-46d4-b012-e812f56a8db1&source=en&target=de&input=Hello, how are you today?&input=I am good today
API base URI –https://api-translate.systran.net
API URL Path– /translation/text/translate
Query Strings: key=*******************b5-e77bbbf5168e&source=en&target=de&input=Hello, how are you today?&input=I am good today
We can pass multiple input parameters for translation like the above query string sample. The maximum length for the input parameter is 50000 paragraphs / 50 MB maximum per request. It can also be increased after a discussion with the SYSTRAN support team.
This API needs a key as a URL parameter which we can store in custom settings or custom metadata types. For this post, I have stored the key in custom metadata ExternalAPIConfiguration__mdt. Create an APIKey__c field in this custom metadata.
|Field Label||Field API||Field Type|
We can create a MetadataHelper class that will hold all methods to read configuration from custom metadata.
getExternalAPISetting:- This method will give API settings for specific APIs.
getTranslationlanguages: This method will return all languages for which we need translation support. We have created a custom metadata TranslationLanguage__mdt in the blog Low Code Integration for Text Translation using SYSTRAN API
Create an apex class SysTranService for API integration. This class will integrate SYSTRAN API and will provide translation support.
TranslationSerialization – This class is used for the deserialization of the response body into an Apex object.
Create a controller class TranslatorController that will be called by Lightning Web Component for getting text translated in the required language. We will use an object wrapper in this class’s method getTranslatedText. We have a separate blog for Sending Wrapper objects to Apex from LWC. Check out this post to understand using object wrappers in LWC and Apex.
6. Test Text Translation using Lightning Web Component
Create a lightning web component to test the above apex class functionality. We have put select control to show language and text area to enter source and target text information on LWC.