Skip to content

Commit

Permalink
Integrate official DeepL java Library
Browse files Browse the repository at this point in the history
  • Loading branch information
splywaczyk committed Mar 23, 2023
1 parent 2215403 commit 6a2e23c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 92 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ Bitcoin-Factory/Test-Client/notebooks/ray_results/
Platform/WebServer/externalScripts/jquery-3.6.0.js
Platform/WebServer/externalScripts/jquery-ui.js
Platform/WebServer/externalScripts/jquery-3.6.0.js

# Temporary translations
*_translated.json
18 changes: 13 additions & 5 deletions Translator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ It automatically translates the language translation files in the Superalgos pro
* Docs-Paragraph-Styles

### How is the translation done?
https://deepl.com is used for translations. There is no API KEY requirement during the translation process.
https://deepl.com is used for translations. Normally DeepL needs authorization key which can be obtained for free but requires registration.
Currently there is a hardcoded authorization key used, but if you want to use your own you have to replace it in the `TranslatorRunBean.java` file:

```
Translator translator = new Translator("e6ec7277-1288-6ac3-3451-db77c52900b0:fx");
```

### Does the original file structure get corrupted during translation?
No. In the project, many checks are made to ensure that the json file structure is not corrupted.
Expand All @@ -27,7 +32,8 @@ No. In the project, many checks are made to ensure that the json file structure
* Windows 10+ (It does not work on Linux operating system yet)
* Java 11+ (You can download it from https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
* https://java.tutorials24x7.com/blog/how-to-install-java-11-on-windows
* Chromedriver.exe (You can download it from https://chromedriver.storage.googleapis.com/102.0.5005.61/chromedriver_win32.zip.)
* Chromedriver.exe (Go to https://chromedriver.chromium.org/downloads and download version that matches your browser. To check your version go to the Help -> About Google Chrome
![Chrome Version](./doc-resources/chrome_version.png))
* Copy chromedriver.exe in the zip file to C:\Windows\System32\ OR a desired location and add the folder containing chromedriver.exe to the "PATH" Environment Variable. For help, you can visit https://www.computerhope.com/issues/ch000549.htm.)


Expand All @@ -37,11 +43,12 @@ No. In the project, many checks are made to ensure that the json file structure
* Run `cd .\bin`
* `java -jar translator.jar` command to see the usage and list of supported languages.
* Start the translate process with `java -jar translator.jar <root folder> <language code>` command.
* Example: `java -jar translator.jar C:\Superalgos tr-TR`
* Example: `java -jar translator.jar C:\Superalgos pl`
* If you want, you can translate only for a specific module.
* Example: `java -jar translator.jar C:\Superalgos\Projects\Foundations tr-TR`
* Example: `java -jar translator.jar C:\Superalgos\Projects\Foundations pl`
* Depending on the folder you give as a parameter, the process may take longer.
* During the translation process, "_translated.json" files are added where the original files are located.
* During the translation process, "<original_name>_translated.json" files are added where the original files are located.

![File structure](./doc-resources/files.png)
* Perform your translation checks in the file with the suffix "_translated.json" and correct the necessary parts.
![Control](./doc-resources/control.png)
Expand All @@ -53,5 +60,6 @@ No. In the project, many checks are made to ensure that the json file structure
* Probably nothing will happen. When you restart it, it will pick up where it left off.

### Restrictions
* Translator uses official DeepL Java Lib: https://github.com/DeepLcom/deepl-java
* Translator does not need the API key. If you do a lot of translations, you may get stuck in daily limits. In this case, it can continue to work from a different internet IP address (I use a VPN).
* In some cases, errors may occur during translation. For this reason, some files cannot be translated. This is not a big problem. Because if you run Translator again, it will translate the files that were skipped without translation.
Binary file added Translator/doc-resources/chrome_version.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions Translator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>com.deepl.api</groupId>
<artifactId>deepl-java</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.devosonder.superalgostranslate.app.config;

import com.devosonder.deepltranslator.DeepLTranslator;
import com.devosonder.deepltranslator.TargetLanguage;
import com.deepl.api.*;
import com.devosonder.superalgostranslate.app.factory.TranslatorFactory;
import com.devosonder.superalgostranslate.app.service.*;
import com.devosonder.superalgostranslate.app.util.FileUtil;
Expand All @@ -21,41 +20,35 @@ public class TranslatorRunBean {

@SneakyThrows
public void run(String[] args) {
Translator translator = new Translator("e6ec7277-1288-6ac3-3451-db77c52900b0:fx");
List<GlossaryLanguagePair> glossaryLanguages = translator.getGlossaryLanguages();

if (args.length == 2 && args[1].equals("apply-translations")) {
var dir = args[0];
changeFileNamesService.change(dir);
System.exit(0);
} else if (args.length < 2) {
System.out.println("java -jar translator.jar <rootFolderName> <targetLanguage>");
System.out.println("Supported Target languages: ");
StringBuilder languages = new StringBuilder();
for (TargetLanguage value : TargetLanguage.values()) {
languages.append(value.getLanguageCode()).append(" ");
System.out.println("Syntax:");
System.out.println("java -jar translator.jar <root folder> <language code>");
System.out.println("Set <language code> to one of the following:");
String sourceLanguage = "en";
for (GlossaryLanguagePair glossaryLanguage : glossaryLanguages) {
if (!sourceLanguage.equals(glossaryLanguage.getSourceLanguage())) {
continue;
}
System.out.printf("%s\n", glossaryLanguage.getTargetLanguage());
}
System.out.println(languages);
System.exit(1);
}
var dir = args[0];
TargetLanguage targetLanguage = null;
try {
targetLanguage = TargetLanguage.getLanguage(args[1]).get();
} catch (Exception e) {
System.out.println("Invalid language.\nSupported Target languages: ");
StringBuilder languages = new StringBuilder();
for (TargetLanguage value : TargetLanguage.values()) {
languages.append(value.getLanguageCode()).append(" ");
}
System.out.println(languages);
System.exit(1);
}
String targetLanguage = args[1];

System.out.println("Translating from: " + dir);

System.out.println("[ ] Looking for folders for translation...");
List<String> allDirectories = FileUtil.getAllDirectories(dir);
System.out.println("[OK] Looking for folders for translation...");
System.out.println("[ ] Translate starting. Please wait...");
var translator = TranslatorFactory.getTranslator();
translator.awaitTermination(10, TimeUnit.SECONDS);
nodeService.setTranslator(translator);
tutorialService.setTranslator(translator);
topicService.setTranslator(translator);
Expand Down Expand Up @@ -89,7 +82,7 @@ public void run(String[] args) {
}
}
} finally {
DeepLTranslator.shutdown();
//
}
System.out.println("[OK] Translate completed");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.devosonder.superalgostranslate.app.service;

import com.devosonder.deepltranslator.DeepLTranslator;
import com.devosonder.deepltranslator.SourceLanguage;
import com.devosonder.deepltranslator.TargetLanguage;
import com.deepl.api.*;
import com.devosonder.superalgostranslate.app.factory.ObjectMaperFactory;
import com.devosonder.superalgostranslate.app.model.common.Paragraph;
import com.devosonder.superalgostranslate.app.model.common.Translation;
Expand All @@ -22,14 +20,14 @@

public class NodeService {
private final List<String> blackList = BlackListUtil.blackListStyleValues;
private DeepLTranslator translator = null;
private Translator translator = null;

public void setTranslator(DeepLTranslator translator) {
public void setTranslator(Translator translator) {
this.translator = translator;
}

@SneakyThrows
public String translate(String rootFolderName, TargetLanguage targetLanguage) {
public String translate(String rootFolderName, String targetLanguage) {
List<String> allFiles = FileUtil.getAllFiles(rootFolderName);
try {
for (String filePath : allFiles) {
Expand Down Expand Up @@ -80,7 +78,7 @@ public String translate(String rootFolderName, TargetLanguage targetLanguage) {
}

@SneakyThrows
private boolean translateParagraphs(Node node, TargetLanguage targetLanguage) {
private boolean translateParagraphs(Node node, String targetLanguage) {
if (node.getParagraphs() == null) {
return false;
}
Expand Down Expand Up @@ -110,13 +108,13 @@ private boolean translateParagraphs(Node node, TargetLanguage targetLanguage) {
}

Translation translation = new Translation();
translation.setLanguage(targetLanguage.getShortLanguageCode());
translation.setLanguage(targetLanguage);
translation.setUpdated(Date.from(new Date().toInstant()).getTime());
String translated;
try {
translated = translator.translate(paragraphText, SourceLanguage.ENGLISH, targetLanguage);
translated = (translator.translateText(paragraphText, "en", targetLanguage)).getText();
isEdited = true;
} catch (IllegalStateException | TimeoutException | SessionNotCreatedException e) {
} catch (IllegalStateException | TimeoutException | SessionNotCreatedException | InterruptedException | DeepLException e) {
e.printStackTrace();
continue;
}
Expand All @@ -134,7 +132,7 @@ private boolean translateParagraphs(Node node, TargetLanguage targetLanguage) {
return isEdited;
}

private boolean translateDefinition(Node node, TargetLanguage targetLanguage) {
private boolean translateDefinition(Node node, String targetLanguage) {
if (node.getDefinition() == null) {
return false;
}
Expand All @@ -154,12 +152,12 @@ private boolean translateDefinition(Node node, TargetLanguage targetLanguage) {
}

Translation translation = new Translation();
translation.setLanguage(targetLanguage.getShortLanguageCode());
translation.setLanguage(targetLanguage);
translation.setUpdated(Date.from(new Date().toInstant()).getTime());
String translated;
try {
translated = translator.translate(definitionText, SourceLanguage.ENGLISH, targetLanguage);
} catch (IllegalStateException | TimeoutException e) {
translated = (translator.translateText(definitionText, "en", targetLanguage)).getText();
} catch (IllegalStateException | TimeoutException | InterruptedException | DeepLException e) {
return false;
}
if (!definitionText.endsWith("\n")) {
Expand All @@ -175,9 +173,9 @@ private boolean translateDefinition(Node node, TargetLanguage targetLanguage) {
return true;
}

private boolean turkishTranslationExists(ArrayList<Translation> translations, TargetLanguage targetLanguage) {
private boolean turkishTranslationExists(ArrayList<Translation> translations, String targetLanguage) {
for (Translation translation : translations) {
if (translation.language.equals(targetLanguage.getShortLanguageCode())) {
if (translation.language.equals(targetLanguage)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.devosonder.superalgostranslate.app.service;

import com.devosonder.deepltranslator.DeepLTranslator;
import com.devosonder.deepltranslator.SourceLanguage;
import com.devosonder.deepltranslator.TargetLanguage;
import com.deepl.api.*;
import com.devosonder.superalgostranslate.app.factory.ObjectMaperFactory;
import com.devosonder.superalgostranslate.app.model.common.Paragraph;
import com.devosonder.superalgostranslate.app.model.common.Translation;
Expand All @@ -25,14 +23,14 @@
public class ReviewService {

private final List<String> blackList = BlackListUtil.blackListStyleValues;
private DeepLTranslator translator = null;
private Translator translator = null;

public void setTranslator(DeepLTranslator translator) {
public void setTranslator(Translator translator) {
this.translator = translator;
}

@SneakyThrows
public void translate(String rootFolderName, TargetLanguage targetLanguage) {
public void translate(String rootFolderName, String targetLanguage) {
List<String> allFiles = FileUtil.getAllFiles(rootFolderName);
try {
for (String filePath : allFiles) {
Expand Down Expand Up @@ -84,7 +82,7 @@ public void translate(String rootFolderName, TargetLanguage targetLanguage) {

}

private boolean translateParagraphs(Review review, TargetLanguage targetLanguage) {
private boolean translateParagraphs(Review review, String targetLanguage) {
if (review.getParagraphs() == null) {
return false;
}
Expand Down Expand Up @@ -114,13 +112,13 @@ private boolean translateParagraphs(Review review, TargetLanguage targetLanguage
}

Translation translation = new Translation();
translation.setLanguage(targetLanguage.getShortLanguageCode());
translation.setLanguage(targetLanguage);
translation.setUpdated(Date.from(new Date().toInstant()).getTime());
String translated;
try {
translated = translator.translate(paragraphText, SourceLanguage.ENGLISH, targetLanguage);
translated = (translator.translateText(paragraphText, "en", targetLanguage)).getText();
isEdited = true;
} catch (IllegalStateException | TimeoutException | SessionNotCreatedException e) {
} catch (IllegalStateException | TimeoutException | SessionNotCreatedException | InterruptedException | DeepLException e) {
e.printStackTrace();
continue;
}
Expand All @@ -138,7 +136,7 @@ private boolean translateParagraphs(Review review, TargetLanguage targetLanguage
return isEdited;
}

private boolean translateDefinition(Review review, TargetLanguage targetLanguage) {
private boolean translateDefinition(Review review, String targetLanguage) {
if (review.getDefinition() == null) {
return false;
}
Expand All @@ -158,12 +156,12 @@ private boolean translateDefinition(Review review, TargetLanguage targetLanguage
}

Translation translation = new Translation();
translation.setLanguage(targetLanguage.getShortLanguageCode());
translation.setLanguage(targetLanguage);
translation.setUpdated(Date.from(new Date().toInstant()).getTime());
String translated;
try {
translated = translator.translate(definitionText, SourceLanguage.ENGLISH, targetLanguage);
} catch (IllegalStateException | TimeoutException e) {
translated = (translator.translateText(definitionText, "en", targetLanguage)).getText();
} catch (IllegalStateException | TimeoutException | InterruptedException | DeepLException e) {
return false;
}
if (!definitionText.endsWith("\n")) {
Expand All @@ -178,9 +176,9 @@ private boolean translateDefinition(Review review, TargetLanguage targetLanguage
return true;
}

private boolean turkishTranslationExists(ArrayList<Translation> translations, TargetLanguage targetLanguage) {
private boolean turkishTranslationExists(ArrayList<Translation> translations, String targetLanguage) {
for (Translation translation : translations) {
if (translation.language.equals(targetLanguage.getShortLanguageCode())) {
if (translation.language.equals(targetLanguage)) {
return true;
}
}
Expand Down
Loading

0 comments on commit 6a2e23c

Please sign in to comment.