Translate Chatter Feed | Text Translation API

banner_1

Text translation API can dynamically translate the different languages:

In this post, I am going to use Yandex Translate Text API to translate chatter feed(text). It uses a hybrid model of machine translation that includes both neural network (deep learning) and statistical approaches.

Yandex Translate Text API is a free API that we can use for translation. You just need to signup to get APIKEY.

API Request syntax:

https://translate.yandex.net/api/v1.5/tr.json/translate
? key=<API key>
& text=<text to translate>
& lang=<translation direction>
& [format=<text format>]
& [options=<translation options>]
& [callback=<name of the callback function>]

Query Parameters Description
key APIKEY: It is issued free of charge.
text The text to translate. You can use multiple text parameters in a request. The source text must be URL-encoded.
lang The translation direction. You can set it in either of the following ways: As a pair of language codes separated by a hyphen (“from”-“to”). For example, en-hi indicates translating from English to Hindi.
format Text format. plain – Text without markup (default value). html – Text in HTML format.
options The only option available at this time is whether the response should include the automatically detected language of the text being translated. This corresponds to the value 1 for this parameter.
callback The name of the callback function. Use for getting a JSONP response.

Chatter feed translation example code: 

Trigger on FeedItem sObject: 

trigger FeedTranslateTrigger on FeedItem (after insert) {
Map<String,String> feedMap = new Map<String,String>();
if(Trigger.isAfter){
if(Trigger.isInsert){
for(FeedItem fi: Trigger.new){
feedMap.put(fi.Id, fi.Body);
}
if(ChatterFeedTranslateCntrl.isFirstTime){
ChatterFeedTranslateCntrl.isFirstTime = false;
ChatterFeedTranslateCntrl.translateFeed(feedMap);
}
}
}
}
view raw FeedTranslateTrigger hosted with ❤ by GitHub

ChatterFeedTranslateCntrl

public class ChatterFeedTranslateCntrl {
public static Boolean isFirstTime = true;
@Future(callout=true)
public static void translateFeed(Map<String,String> feedMap) {
String feedBody = feedMap.values()[0].replaceAll('<[/a-zAZ0-9]*>','');
feedBody = feedBody.replace(' ','%20');
system.debug('feedBody = '+feedBody);
Set<String> feedItemIds = feedMap.keySet();
List<FeedItem> fItemList = [select Id,Body from FeedItem where id IN:feedItemIds];
system.debug('fItemList: ' +fItemList);
HttpRequest reqest = new HttpRequest();
String APIKey = 'trnsl.1.1.20191130T125129Z.74852570ba317630.62b74fb91590782bc383c79786987364XXXXXXXXXX'; //APIKey
String langTranslation = 'en-hi';//english into Hindi
String endPointURL = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key='+APIKey+'&text='+feedBody+'&lang='+langTranslation+'&%5Bformat=plain&#39;;
reqest.setEndpoint(endPointURL);
reqest.setMethod('GET');
reqest.setHeader('Accept','application/json');
Http h = new Http();
HTTPResponse response = h.send(reqest);
system.debug('response: '+response.getBody());
string jsonResponse = response.getBody().replace('[','');
jsonResponse = jsonResponse.replace(']','');
system.debug('jsonResponse; '+jsonResponse);
// Parse JSON response to get all the totalPrice field values.
JSONParser parser = JSON.createParser(jsonResponse);
String feedBodyStr = '';
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'text')) {
// Get the value.
parser.nextToken();
// Compute the grand total price for all invoices.
feedBodyStr = parser.getText();
}
}
system.debug('feedBodyStr=' + feedBodyStr);
fItemList[0].Body = feedBodyStr;
update fItemList;
}
}

 

Thanks

Arun Kumar

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s