Language IO API (Salesforce Managed Package)
The Language IO API managed package for Salesforce allows organizations to use pre-built language detection and translation Apex actions within Apex code, Flows, and Einstein Bots. These actions call Language IO's detection and translation services from within your Salesforce org.
Installing the Package
- Choose the appropriate package link depending on your org type:
- Log in with your Salesforce administrator user, if prompted.
- Select "Install for all users", and click "Install".
Using the Language IO API Apex Actions
Apex Code
The following examples show how to use the methods from the Language IO API managed package to do on-demand Language Detection and Translation. The // comments in each snippet explain the different commands.
Language Detection
Lio.DetectionAPI.Request req = new Lio.DetectionAPI.Request(); // create new request req.namedCredential = 'LIO'; // name of the named credential used for the connection to the LIO server req.routableId = '123'; // unique identifier. For example, record Idreq.sContent = 'Bonjour'; // content for which the language needs to be detected
List<Lio.DetectionAPI.Result> resultList = Lio.DetectionAPI.detectLanguage(new List<Lio.DetectionAPI.Request>{req}); // make requestsystem.debug(resultList.get(0).sDetectedLanguage); // the result should be 'fr'
Translation
Here is a code example for a one-time translation
Lio.TranslationAPI.Request req = new Lio.TranslationAPI.Request(); // create new requestreq.namedCredential = 'LIO'; // name of the named credential used for the connection to the LIO server req.routableId = '123'; // unique identifier. For example, record Idreq.sContent = 'Hello'; // content for which the language needs to be detectedreq.sSourceLocale = 'en'; //source language. This is optionalreq.sTargetLocale = 'fr'; // target language
List<Lio.TranslationAPI.Result> resultList = Lio.TranslationAPI.translate(new List<Lio.TranslationAPI.Request>{req}); // make requestsystem.debug(resultList.get(0).sTranslation); // the result should be 'Bonjour'
Flows
The Language Detection and Translation methods are "Invocable Methods". This means that you can use them in automation processes, such as Flows.
To call either the Language Detection or Translation functionalities in a Flow, add an Action element:
In the list of flow actions, find the LIO__DetectionAPI / LIO__TranslationAPI apex invocable methods:
Language Detection
(1) - Named Credential for your Language IO consumer
(2) - Unique ID (This can be the record ID)
(3) - Text for which you want to detect the language
The output is called 'sDetectedLanguage'.
Translation
(1) - Named Credential for your Language IO consumer
(2) - Unique ID (This can be the record ID)
(3) - Text to be translated
(4) - Target language
(5) - (Optional) Source language
The output is called ‘sTranslation’.
Einstein Bots
Similar to Flows, the Language Detection and Translation functions can be used with Einstein bots.
Language Detection
In the following example, you use Language detection to determine the visitor’s language from the pre-chat subject and then potentially route the visitor to a bot configured with the necessary language.
(1) - Text for which you want to determine the language. In this example, this is the pre-chat Subject
(2) - Named Credential for your Language IO consumer
(3) - Unique ID
(4) - Detected language
Translation
In the following example, you use the Translation functionality to translate an order status retrieved from Salesforce into the visitor’s language.
Apex Actions
Settings
(1) - Source language (In this example, the language of the retrieved order status.)
(2) - Text to be translated (In this example, the order status.)
(3) - Target language (In this example, the visitor’s language.)
(4) - Named Credential for your Language IO consumer
(5) - Unique ID
(6) - Translated text (In this example, the order status in the visitor's language.)