How to detect the language of a string in Salesforce Apex

Judicious use of language detection can provide great information about your customer base, or be used in conjunction with a translation system to open up some global dialogue. Whichever purpose is yours, you are going to need a workable solution to get things up and running in Apex. We’ve got you covered.

We shall start things off by downloading and extracting our API client into our project folder.

With that out of the way, our languageDetectionGetLanguage is now available to be called:

SwagLanguageDetectionApi api = new SwagLanguageDetectionApi();SwagClient client = api.getClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');Apikey.setApiKey('YOUR API KEY');Map<String, Object> params = new Map<String, Object>{'input' => SwagLanguageDetectionRequest.getExample()};try {// cross your fingersSwagLanguageDetectionResponse result = api.languageDetectionGetLanguage(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

Feed in your text string, and voila, the language has been identified! Here is a test result on the phrase “Ende gut alles gut.”

"Successful": true,
"DetectedLanguage_ThreeLetterCode": "DEU",
"DetectedLanguage_FullName": "German"

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store