For translating Languages, we are trusting Google for years and it is also a very dependable and accurate so what if we can use that service in salesforce itself. wouldn’t it be so amazing? yeahhh, you are right. we can use google translate service in salesforce and it doesn’t need very complex coding and configuration.
so let’s start now-
first we need to create a project in our google account as per the image below
In API Library, search for Cloud Translation API and activate it it will ask for billing information and you need to add your payment method or card information then you can activate this service. Probably they will give you first free 300$ in your account for testing these services and they will not debit any money without your permission.
Then you need to create credentials in that API and that will be called API key which we will use for authentication from the third party.
Now everything is set up on Google Translate API side now we need to code on salesforce side.
For calling out google api, we have to add remote side setting with this URl: https://www.googleapis.com
If you will check the URL then it has source and target language both, because when we define our source language then Google doesn’t need to detect the language so they will charge less for the translation.
you can use below code in developer console and get the result in debug-
String a = 'I'm getting bombarded with scam calls threatening to arrest me.';
String text = a.replace(' ', '%20');
//Change your key here
String Key = 'AIzaSyB***************YKi-4hpZ1Dgvo';
HttpRequest req = new HttpRequest();
Http http = new Http();
string url = 'https://www.googleapis.com/language/translate/v2?key='+key+'&source=en&target=es&q=' + text;
System.debug('URL ' + url);
String jsonResults = '';
HTTPResponse resp = http.send(req);
jsonResults = resp.getBody();
we are using version 2 of this API but they also introduced new version 3 which has additional features like a custom dictionary and Batch requests. if you need those functionalities then you can go with version 3.