Java program to translate language

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Java API for translating texts across different providers

License

sitepark/translate-api

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A Java API to translate texts with various translation services. The following services are supported

dependency> groupId>com.siteparkgroupId> artifactId>translate-apiartifactId> version>1.0.0version> dependency>

Select language and provider

The API supports multiple providers that can translate the text. For translation, the provider type must be specified along with the source and target languages.

Читайте также:  Datetime parameter in php

For this purpose a TranslationLanguage object is created.

TranslationLanguage language = TranslationLanguage.builder() .providerType(SupportedProvider.LIBRE_TRANSLATE) .source("de") .target("en") .build();

A configuration must be stored for the specified provider, which can be set via the TranslatorConfiguration.Builder .

LibreTranslateTranslationProviderConfiguration providerConfig = LibreTranslateTranslationProviderConfiguration.builder() .url(. ) .apiKey(. ) .build(); TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() .translationProviderConfiguration(providerConfig) . .build();

Translate a singe String/Html

TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() .providerConfig(. ) . .build(); StringTranslator translator = StringTranslator.builder() .translatorConfiguration(translatorConfiguration) .build(); TranslationLanguage language = TranslationLanguage.builder() .providerType(SupportedProvider.LIBRE_TRANSLATE) .source("de") .target("en") .build(); // translate string String result = translator.translate(language, "Hallo"); // translate html String htmlResult = translator.translate(language, "Hallo");
TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() .providerConfig(. ) . .build(); StringArrayTranslator translator = StringArrayTranslator.builder() .translatorConfiguration(translatorConfiguration) .build(); TranslationLanguage language = TranslationLanguage.builder() .providerType(SupportedProvider.LIBRE_TRANSLATE) .source("de") .target("en") .build(); String[] result = translator.translate( language, new String[] "Hallo", "Welt">);
TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() .providerConfig(. ) . .build(); TranslationLanguage language = TranslationLanguage.builder() .providerType(SupportedProvider.LIBRE_TRANSLATE) .source("de") .target("en") .build(); String sourceJson = . String targetJson = translator.translate(language, sourceJson);
< "headline": "Überschrift", "text": "Ein schöner Text", "items": [ < "text": "Blumen", "number": 10, "boolean": true > ] >
< "headline": "Heading", "text": "A beautiful text", "items": [ < "text": "Flowers", "number": 10, "boolean": true > ] >

Translate all JSON-Files in a directory structure

Is the following directory structure given

basedir/ ├─ de/ ├─ a.json ├─ b/ ├─ c.json 

The following code translates all json files and puts them in a parallel directory.

TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() .providerConfig(. ) . .build(); JsonFileListTranslator jsonFileListTranslator = JsonFileListTranslator.builder() .dir(Paths.get("basedir")) .output(Paths.get("output/de.translated")) .sourceLang("de") .targetLangList("en") .translatorConfiguration(translatorConfiguration) .build(); jsonFileListTranslator.translate(SupportedProvider.LIBRE_TRANSLATE);

The translations are stored in this structure

output/ ├─ de.translated/ ├─ en/ ├─ a.json ├─ b/ ├─ c.json 

With the caching implementation you can prevent that already translated texts are translated again. This can be useful if, for example, a list contains many texts, but only individual texts have been updated. With the help of the cache, the entire list can be retranslated, whereby only the changed texts are passed to libretranslate.

Читайте также:  Java hashmap add key

For this purpose the TranslationCache interface is implemented.

public interface TranslationCache < public OptionalString> translate(String sourceText); public void update(Listextends TranslatableText> translated); >

An example here is the TranslationFileCache implementation. An example here is the TranslationFileCache implementation. Together with the JsonFileListTranslator only the texts within the JSON files that have changed are retranslated.

Path output = Paths.get("output/de.translated"); TranslationFileCache translationCache = new TranslationFileCache( output.resolve(".translation-cache")); TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() .providerConfig(. ) . .build(); JsonFileListTranslator jsonFileListTranslator = JsonFileListTranslator.builder() .dir(Paths.get("basedir")) .output(output) .translationCache(translationCache) .sourceLang("de") .targetLangList("en") .translatorConfiguration(translatorConfiguration) .build(); jsonFileListTranslator.translate(SupportedProvider.LIBRE_TRANSLATE);

To retrieve information about the translation process, the TranslationListener interface can be implemented.

public interface TranslationListener < void translated(TranslationEvent event); >

The listener is set in the TranslatorConfiguration .

TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() //. .translationListener(. ) //. .build();

The information can be queried via the TranslationEvent object.

event.getTranslationLanguage() — Source and target language of the translation. event.getTranslationTime() — Time in milliseconds that the translation took. event.getChunks() — Number of texts passed to libretranslate in an array. event.getSourceBytes() — Number of bytes that were translated. event.getTargetBytes() — Number of bytes of the translated text.

In the case that language packages of applications are to be translated, the case occurs that these texts contain placeholders, which are only filled in at runtime in the application.

With placeholders of the form $ <. >and <. >it can be prevented that these are also translated. This way the placeholders remain in the translated text.

TranslatorConfiguration translatorConfiguration = TranslatorConfiguration.builder() //. .encodePlaceholder(true) //. .build();

About

Java API for translating texts across different providers

Читайте также:  Css text shadow drop shadow

Источник

How to Translate a Language in Java

Join the DZone community and get the full member experience.

Since 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:

Источник

Оцените статью