Skip to content

Commit

Permalink
* New DeepL Translator API
Browse files Browse the repository at this point in the history
  • Loading branch information
cbgc5 committed Apr 18, 2020
1 parent 7fc75b6 commit bbe9551
Show file tree
Hide file tree
Showing 38 changed files with 710 additions and 8,967 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
93 changes: 36 additions & 57 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,77 +1,56 @@
# DeepLTranslator
> This project is a simple <a href="https://www.deepl.com/translator">DeepL Translator</a> 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.
> This project is a simple <a href="https://www.deepl.com/translator">DeepL Translator</a> API written in Java. It translates via the DeepL website sentences. This works without having a premium access and can be used free of charge.
## Requires
* Java 9
## Examples
### Using a configuration
```java
DeepLConfiguration deepLConfiguration = new DeepLConfiguration.Builder()
.setTimeout(Duration.ofSeconds(10))
.setRepetitions(3)
.setRepetitionsDelay(retryNumber -> Duration.ofMillis(3000 + 5000 * retryNumber))
.build();

## Code Example
### Synchronous translating
To translate texts synchronously, please use this method:<br />
`DeepLTranslator#translate(String text, Language sourceLanguage, Language targetLanguage)`
DeepLTranslator deepLTranslator = new DeepLTranslator(deepLConfiguration);
```

Example:
### Synchronous translating
```java
Translation translation = DeepLTranslator.translate("Hello, my name is Linus. And what's your name?", Language.ENGLISH, Language.GERMAN);

if(translation.printError()) {
return;
try {
String translation = deepLTranslator.translate("I ran into a similar problem yesterday.", Language.ENGLISH, Language.GERMAN);
System.out.println(translation);
} catch (Exception e) {
e.printStackTrace();
}

translation.getTranslations().forEach(System.out::println);
```

### Asynchronous translating
To translate texts asynchronously, please use this method:<br />
`DeepLTranslator#asyncTranslate(String text, Language sourceLanguage, Language targetLanguage, TranslationConsumer translationConsumer)`

Example:
```java
DeepLTranslator.asyncTranslate("Hello, my name is Linus. And what's your name?", Language.ENGLISH, Language.GERMAN, (translation, exception) -> {
if(translation != null) {
if(translation.printError()) {
return;
}

translation.getTranslations().forEach(System.out::println);
} else if(exception != null) {
exception.printStackTrace();
deepLTranslator.translateAsync("Hello, guys. My name is Linus.", Language.ENGLISH, Language.GERMAN, new TranslationConsumer() {
@Override
public void handleTranslation(String translation) {
System.out.println(translation);
}
});
```

### Changing Timeout Duration
Default timeout duration is 10 seconds.<br /><br />
To change the timeout duration, please use this method:<br />
`DeepLTranslator#setTimeoutDuration(Duration timeoutDuration)`

Example:
```java
DeepLTranslator.setTimeoutDuration(Duration.ofSeconds(5));
```

### Change Waiting Duration For Repeating Request At Error 1042901/"Too many requests."
Default waiting duration is 3 seconds.<br /><br />
To change the waiting duration for repeating a request, please use this method:<br />
`DeepLTranslator#setRepeatRequestTime(Duration repeatRequestTime)`
@Override
public void handleException(Exception e) {
e.printStackTrace();
}

Example:
```java
DeepLTranslator.setRepeatRequestTime(Duration.ofSeconds(5));
@Override
public void handleFinally() {
System.out.println("Translation/Exception handled.");
}
});
```

### Limit The Number Of Maximum Threads
Default number of maximum threads is Integer.MAX_VALUE.<br /><br />
To limit the number of maximum threads that handle asynchronous translation, use the following method:<br />
`DeepLTranslator#setExecutor(ExecutorService executor)`

Example:
###Shutdown
Stops all running threads.
```java
DeepLTranslator.setExecutor(Executors.newFixedThreadPool(5));
DeepLTranslator.shutdown();
```

### Stopping program
If you want to close your program, you should call this method so that the ExecutorService can close all threads:<br />
`DeepLTranslator#shutdown()`
### Example
* [DeepLTranslatorTest](src/test/java/DeepLTranslatorTest.java)

## License
This project is licensed under the Apache License 2.0 - see the [LICENSE.md](LICENSE.md) file for details
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details
69 changes: 69 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<name>DeepLTranslator</name>
<description>DeepL Translator API for Java</description>

<groupId>de.linus</groupId>
<artifactId>DeepLTranslator</artifactId>
<version>1.0.0</version>

<properties>
<java.version>1.8</java.version>
<jbrowserdriver.version>1.1.1</jbrowserdriver.version>
<guava.version>29.0-jre</guava.version>

<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>com.machinepublishers</groupId>
<artifactId>jbrowserdriver</artifactId>
<version>${jbrowserdriver.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>

<build>
<finalName>${project.name}</finalName>
<defaultGoal>clean package</defaultGoal>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit bbe9551

Please sign in to comment.