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.
Example repository Azure-Translator-Example
The following example translates a simple Hello World to Portuguese, Spanish and French.
public class MyFirstTranslation {
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>";
// Create an Azure API object with your key and 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:");
// Loop through each translation and print the language code and text
response.getTranslations().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: '0.0.6'
Maven
<dependency>
<groupId>io.github.brenoepics</groupId>
<artifactId>at4j</artifactId>
<version>0.0.6</version>
</dependency>
Sbt
libraryDependencies += "io.github.brenoepics" % "at4j" % "0.0.6"
tl;dr Generate Here.
How to generate my own keys?
-
Sign in to the Azure portal.
-
In the left-hand menu, click on "Create a resource".
-
In the "Search the Marketplace" box, type "Translator Text" and select it from the dropdown list.
-
Click on the "Create" button.
-
Fill in the required details:
- Name: Enter a unique name for your resource.
- Subscription: Select the Azure subscription that you want to use.
- Resource Group: You can create a new resource group or select an existing one.
- Pricing tier: Select the pricing tier that suits your needs.
-
Click on the "Review + create" button.
-
Review your settings and click on the "Create" button.
-
After the deployment is complete, go to the resource you just created.
-
In the left-hand menu, click on "Keys and Endpoint".
-
You will see two keys and an endpoint. You can use either of the keys in your application.
Azure Free Tier
Azure offers a free tier for its services, which is a great way to get started with Azure Translator for Java. The free tier includes a monthly allowance of 2 million characters for Translator Text. This is sufficient for testing and small projects.
To use the free tier, you need to create an Azure account and set up a Translator Text resource. Azure will require a credit card for identity verification, but you will not be charged unless you explicitly change your settings and choose to do so.
Please note that the free tier usage limits are subject to change by Azure, and it's recommended to check the current details on the Azure Pricing page.
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 Pricing page.
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.
For example, Log4j Core in Gradle
dependencies { runtimeOnly 'org.apache.logging.log4j:log4j-core:2.19.0' }
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.