Detect the Language of Text using NLP in Java

2 min readJul 21, 2021


Establishing a safe and accessible environment for all users of an online platform is a top priority these days. If you have a wide base of customers that spans the globe, it is a best practice to install certain tools to ensure connecting with various nationalities is as easy as possible. By leveraging this Natural Language Processing API in Java, you will be able to automatically detect the language a text string is written in, which is a key step in the translation process.

To get things started, we will install the Maven package by adding a reference to the repository in pom.xml:


Then add a reference to the dependency:


Once the installation is complete, we simply input the text string and API key into the following example code:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.LanguageDetectionApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
LanguageDetectionApi apiInstance = new LanguageDetectionApi();
LanguageDetectionRequest input = new LanguageDetectionRequest(); // LanguageDetectionRequest |
try {
LanguageDetectionResponse result = apiInstance.languageDetectionGetLanguage(input);
} catch (ApiException e) {
System.err.println("Exception when calling LanguageDetectionApi#languageDetectionGetLanguage");

If you need to obtain an API key, you can do so by registering for a free account on the Cloudmersive website; this provides 800 monthly calls across any of our APIs.




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