Retrieve the Language for an HTML Document in Java

Cloudmersive
2 min readJul 13, 2021

The language code of an HTML document is an integral element, and should always be declared within the HTML tag. If you need to retrieve the language code, which is a two-letter combination that represents a specific language, we have an API that can be used in Java to automatically do just that.

We will begin with the Maven installation by adding a jitpack reference to the repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Then, we will add a reference to the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>

At this point, we are ready to call the function with the following 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.EditHtmlApi;
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)
//Apikey.setApiKeyPrefix("Token");
EditHtmlApi apiInstance = new EditHtmlApi();
File inputFile = new File("/path/to/inputfile"); // File | Optional: Input file to perform the operation on.
String inputFileUrl = "inputFileUrl_example"; // String | Optional: URL of a file to operate on as input.
try {
HtmlGetLanguageResult result = apiInstance.editHtmlHtmlGetLanguage(inputFile, inputFileUrl);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditHtmlApi#editHtmlHtmlGetLanguage");
e.printStackTrace();
}

The language code will be clearly indicated in the results! To retrieve your API key, head to the Cloudmersive website to register for a free account; this will provide 800 monthly calls across our library of APIs.

--

--

Cloudmersive

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