An unofficial Java library for translating text using Azure AI Cognitive Services.
- Text Translation: Translate text from one language (or detect) to another or to a list of languages.
- Language Detection: Detect the language of a given text.
- Profanity Handling: Options for handling profanity in translations.
- Text Type Support: We support both plain text and HTML text translation.
- And more.
Note
Example repository Azure-Translator-Example
The following example translates a simple Hello World to Portuguese, Spanish and French.
public class ExampleTranslator {
public static void main(String[] args) {
// Insert your Azure key and region here
String azureKey = "<Your Azure Subscription Key>";
String azureRegion = "<Your Azure Subscription Region>";
AzureApi api = new AzureApiBuilder().setKey(azureKey).region(azureRegion).build();
// Set up translation parameters
List<String> targetLanguages = List.of("pt", "es", "fr");
TranslateParams params = new TranslateParams("Hello World!", targetLanguages).setSourceLanguage("en");
// Translate the text
Optional<TranslationResponse> translationResult = api.translate(params).join();
// Print the translations
translationResult.ifPresent(response -> {
System.out.println("Translations:");
// Create a single stream of translations from all results
response.getResultList()
.stream()
.flatMap(result -> result.getTranslations().stream())
.forEach(translation -> System.out.println(translation.getLanguageCode() + ": " + translation.getText()));
});
}
}
Expected Output
Translations:
pt: Olá, Mundo!
es: ¡Hola mundo!
fr: Salut tout le monde!
The recommended way to get AT4J is to use a build manager, like Gradle or Maven.
Gradle
implementation group: 'io.github.brenoepics', name: 'at4j', version: '1.0.0'
Maven
<dependency>
<groupId>io.github.brenoepics</groupId>
<artifactId>at4j</artifactId>
<version>1.0.0</version>
</dependency>
Sbt
libraryDependencies += "io.github.brenoepics" % "at4j" % "1.0.0"
Warning
Remember to keep your keys secure and do not share them publicly. If you believe that a key has been compromised, you must regenerate it in Azure's Panel. For more information, visit the Azure portal.
Any Log4j-2-compatible logging framework can be used to provide a more sophisticated logging experience with being able to configure log format, log targets (console, file, database, etc.), log levels per class, and much more.
More info at our Docs
The version number has a 3-digit format: major.minor.trivial
major
: Increased extremely rarely to mark a major release (usually a rewrite affecting very huge parts of the library).minor
: Any backward incompatible change to the api wrapper.trivial
: A backward compatible change to the api wrapper. This is usually an important bugfix (or a bunch of smaller ones) or a backwards compatible feature addition.
A method or class that is marked as deprecated can be removed with the next minor release (but it will usually stay for several minor releases). A minor release might remove a class or method without having it deprecated, but we will do our best to deprecate it before removing it. We are unable to guarantee this though, because we might have to remove / replace something due to changes made by Azure, which we are unable to control. Usually you can expect a deprecated method or class to stay for at least 6 months before it finally gets removed, but this is not guaranteed.
Contributions of any kind are welcome. You can start contributing to this library by creating issues, submitting pull requests or giving a star to the project.
AT4J is distributed under the Apache license version 2.0.