This project is a simple DeepL Translator API written in Java. It translates via the DeepL server sentences. This works without having a premium access and can be used free of charge.
- Java 9
To translate texts synchronously, please use this method:
DeepLTranslator#translate(String text, Language sourceLanguage, Language targetLanguage)
Example:
Translation translation = DeepLTranslator.translate("Hello, my name is Linus. And what's your name?", Language.ENGLISH, Language.GERMAN);
if(translation.printError()) {
return;
}
translation.getTranslations().forEach(System.out::println);
To translate texts asynchronously, please use this method:
DeepLTranslator#asyncTranslate(String text, Language sourceLanguage, Language targetLanguage, TranslationConsumer translationConsumer)
Example:
DeepLTranslator.asyncTranslate("Hello, my name is Linus. And what's your name?", Language.ENGLISH, Language.GERMAN, new TranslationConsumer() {
@Override
public void handleTranslation(Translation translation) {
if(translation.printError()) {
return;
}
translation.getTranslations().forEach(System.out::println);
}
@Override
public void handleException(Exception e) {
e.printStackTrace();
}
});
Default timeout duration is 10 seconds.
To change the timeout duration, please use this method:
DeepLTranslator#setTimeoutDuration(Duration timeoutDuration)
Example:
DeepLTranslator.setTimeoutDuration(Duration.ofSeconds(5));
Default waiting duration is 3 seconds.
To change the waiting duration for repeating a request, please use this method:
DeepLTranslator#setRepeatRequestTime(Duration repeatRequestTime)
Example:
DeepLTranslator.setRepeatRequestTime(Duration.ofSeconds(5));
Default number of maximum threads is Integer.MAX_VALUE.
To limit the number of maximum threads that handle asynchronous translation, use the following method:
DeepLTranslator#setExecutor(ExecutorService executor)
Example:
DeepLTranslator.setExecutor(Executors.newFixedThreadPool(5));
If you want to close your program, you should call this method so that the ExecutorService can close all threads:
DeepLTranslator#shutdown()
This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details