/DeepLTranslator

The DeepL Translator is an API written in Java that translates via the DeepL server sentences.

Primary LanguageJavaApache License 2.0Apache-2.0

DeepLTranslator

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.

Requires

  • Java 9

Code Example

Synchronous translating

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);

Asynchronous translating

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();
    }
});

Changing Timeout Duration

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));

Change Waiting Duration For Repeating Request At Error 1042901/"Too many requests."

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));

Limit The Number Of Maximum Threads

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));

Stopping program

If you want to close your program, you should call this method so that the ExecutorService can close all threads:
DeepLTranslator#shutdown()

License

This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details