How to Translate a Language in Java
Leverage Deep Learning AI to translate text in ENG to FRA, FRA to ENG, ENG to DEU, DEU to ENG, ENG to RUS, or RUS to ENG.
Join the DZone community and get the full member experience.
Join For FreeSince the internet stepped into the spotlight in the late twentieth century, it has been doing its part to facilitate globalization on a massive scale. With companies increasing international transactions and individuals from across the world connecting online, we are all growing closer each day.
While these personal and business connections are teaching us a lot about our various cultures, we still encounter language barriers. These barriers can cause considerable inconvenience and frustration for users and businesses alike, which is why the developments that have been made in language translation technology are so valuable.
This brings us to the translation solution that we will be discussing today; if you need to automate language translation between English and either French, German, or Russian, we have a few APIs to help with that. Leveraging Deep Learning AI, we offer the following natural language processing functions:
- English to French.
- French to English.
- English to German.
- German to English.
- English to Russian.
- Russian to English.
In this article, I will guide you through how to use these APIs in Java. Each API will begin with the same few steps but will alter when it comes to creating an instance of the API.
Now to start things off, we will first need to install the SDK with Maven by adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, we will add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
Our next step is to add the imports to the controller, and configure the API key:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//impot com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.LanguageTranslationApi;
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");
Now, we come to the variable section of the code; this is where you will input your translation request, instance the API, and call the function for your specific request. I will provide each code for our six translation options below.
English (ENG) to French (FRA):
xxxxxxxxxx
LanguageTranslationApi apiInstance = new LanguageTranslationApi();
LanguageTranslationRequest input = new LanguageTranslationRequest(); // LanguageTranslationRequest | Input translation request
try {
LanguageTranslationResponse result = apiInstance.languageTranslationTranslateEngToFra(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling LanguageTranslationApi#languageTranslationTranslateEngToFra");
e.printStackTrace();
}
French (FRA) to English (ENG):
xxxxxxxxxx
LanguageTranslationApi apiInstance = new LanguageTranslationApi();
LanguageTranslationRequest input = new LanguageTranslationRequest(); // LanguageTranslationRequest | Input translation request
try {
LanguageTranslationResponse result = apiInstance.languageTranslationTranslateFraToEng(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling LanguageTranslationApi#languageTranslationTranslateFraToEng");
e.printStackTrace();
}
English (ENG) to German (DEU):
xxxxxxxxxx
LanguageTranslationApi apiInstance = new LanguageTranslationApi();
LanguageTranslationRequest input = new LanguageTranslationRequest(); // LanguageTranslationRequest | Input translation request
try {
LanguageTranslationResponse result = apiInstance.languageTranslationTranslateEngToDeu(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling LanguageTranslationApi#languageTranslationTranslateEngToDeu");
e.printStackTrace();
}
German (DEU) to English (ENG):
xxxxxxxxxx
LanguageTranslationApi apiInstance = new LanguageTranslationApi();
LanguageTranslationRequest input = new LanguageTranslationRequest(); // LanguageTranslationRequest | Input translation request
try {
LanguageTranslationResponse result = apiInstance.languageTranslationTranslateDeuToEng(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling LanguageTranslationApi#languageTranslationTranslateDeuToEng");
e.printStackTrace();
}
English (ENG) to Russian (RUS):
xxxxxxxxxx
LanguageTranslationApi apiInstance = new LanguageTranslationApi();
LanguageTranslationRequest input = new LanguageTranslationRequest(); // LanguageTranslationRequest | Input translation request
try {
LanguageTranslationResponse result = apiInstance.languageTranslationTranslateEngToRus(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling LanguageTranslationApi#languageTranslationTranslateEngToRus");
e.printStackTrace();
}
Russian (RUS) to English (ENG):
xxxxxxxxxx
LanguageTranslationApi apiInstance = new LanguageTranslationApi();
LanguageTranslationRequest input = new LanguageTranslationRequest(); // LanguageTranslationRequest | Input translation request
try {
LanguageTranslationResponse result = apiInstance.languageTranslationTranslateRusToEng(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling LanguageTranslationApi#languageTranslationTranslateRusToEng");
e.printStackTrace();
}
Et c’est tout! Or in English, and that’s all! In order for the request to be successful, you must be sure to correctly input both your translation request and your API key.
Opinions expressed by DZone contributors are their own.
Comments