forked from 5gi/DeepJ
Fixed repetitive unreadable enum key.
Made code less if statementy
This commit is contained in:
parent
2c276acaf9
commit
7958399f0c
14
README.md
14
README.md
@ -1,3 +1,13 @@
|
|||||||
# DeepJ
|
# DeepJ
|
||||||
|
|:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::|
|
||||||
Java Wrapper for DeepL API
|
| ███████╗ ██████╗ ██╗ ██╗ ██╗██████╗ ██████╗ █████╗ ████████╗███████╗██████╗ ██████╗ ███████╗███████╗██████╗ ██╗ |
|
||||||
|
| ██╔════╝██╔════╝ ██║ ██║ ██║██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔══██╗ ██╔══██╗██╔════╝██╔════╝██╔══██╗ ██║ |
|
||||||
|
| ███████╗██║ ███╗██║ ██║ ██║██████╔╝██║ ██║███████║ ██║ █████╗ ██║ ██║ ██║ ██║█████╗ █████╗ ██████╔╝ ██║ |
|
||||||
|
| ╚════██║██║ ██║██║ ██║ ██║██╔═══╝ ██║ ██║██╔══██║ ██║ ██╔══╝ ██║ ██║ ██║ ██║██╔══╝ ██╔══╝ ██╔═══╝██ ██║ |
|
||||||
|
| ███████║╚██████╔╝██║ ╚██████╔╝██║ ██████╔╝██║ ██║ ██║ ███████╗██████╔╝ ██████╔╝███████╗███████╗██║ ╚█████╔╝ |
|
||||||
|
| ╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═════╝ ╚═════╝ ╚══════╝╚══════╝╚═╝ ╚════╝ |
|
||||||
|
| █▀█ █▀█ █ █▀▀ █ █▄ █ ▄▀█ █ █▄▄ █▄█ █▀ ▀█▀ █▀▀ █ █ █▀▀ █▄ █ |
|
||||||
|
| █▄█ █▀▄ █ █▄█ █ █ ▀█ █▀█ █▄▄ █▄█ █ ▄█ █ ██▄ ▀▄▀ ██▄ █ ▀█ |
|
||||||
|
| |
|
||||||
|
| DeepJ is a Java Wrapper for the DeepL Free Public API. This was originally made by Steven (Forked From) and Updated by 5gi |
|
||||||
|
|:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::|
|
@ -1,15 +1,3 @@
|
|||||||
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
|
|
||||||
// ███████╗ ██████╗ ██╗ ██╗ ██╗██████╗ ██████╗ █████╗ ████████╗███████╗██████╗ ██████╗ ███████╗███████╗██████╗ ██╗ //
|
|
||||||
// ██╔════╝██╔════╝ ██║ ██║ ██║██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔══██╗ ██╔══██╗██╔════╝██╔════╝██╔══██╗ ██║ //
|
|
||||||
// ███████╗██║ ███╗██║ ██║ ██║██████╔╝██║ ██║███████║ ██║ █████╗ ██║ ██║ ██║ ██║█████╗ █████╗ ██████╔╝ ██║ //
|
|
||||||
// ╚════██║██║ ██║██║ ██║ ██║██╔═══╝ ██║ ██║██╔══██║ ██║ ██╔══╝ ██║ ██║ ██║ ██║██╔══╝ ██╔══╝ ██╔═══╝██ ██║ //
|
|
||||||
// ███████║╚██████╔╝██║ ╚██████╔╝██║ ██████╔╝██║ ██║ ██║ ███████╗██████╔╝ ██████╔╝███████╗███████╗██║ ╚█████╔╝ //
|
|
||||||
// ╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═════╝ ╚═════╝ ╚══════╝╚══════╝╚═╝ ╚════╝ //
|
|
||||||
// █▀█ █▀█ █ █▀▀ █ █▄ █ ▄▀█ █ █▄▄ █▄█ █▀ ▀█▀ █▀▀ █ █ █▀▀ █▄ █ //
|
|
||||||
// █▄█ █▀▄ █ █▄█ █ █ ▀█ █▀█ █▄▄ █▄█ █ ▄█ █ ██▄ ▀▄▀ ██▄ █ ▀█ //
|
|
||||||
// //
|
|
||||||
// DeepJ is a Java Wrapper for the DeepL Free Public API. This was originally made by Steven (Forked From) and Updated by 5gi //
|
|
||||||
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
|
|
||||||
package tech.nevets.deepj;
|
package tech.nevets.deepj;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@ -25,7 +13,7 @@ public class Translator {
|
|||||||
public Translator(String authKey) {
|
public Translator(String authKey) {
|
||||||
request = new Request(authKey);
|
request = new Request(authKey);
|
||||||
}
|
}
|
||||||
public String translate(Enum<Language> langToTranslateTo, String sourceMessage) {
|
public String translate(Language langToTranslateTo, String sourceMessage) {
|
||||||
String response = "Error processing request";
|
String response = "Error processing request";
|
||||||
try {
|
try {
|
||||||
response = request.get(langToTranslateTo, sourceMessage);
|
response = request.get(langToTranslateTo, sourceMessage);
|
||||||
@ -47,67 +35,52 @@ public class Translator {
|
|||||||
public Request(String authKey) {
|
public Request(String authKey) {
|
||||||
this.authKey = authKey;
|
this.authKey = authKey;
|
||||||
}
|
}
|
||||||
public String get(Enum<Language> langEnum, String message) throws IOException, InterruptedException {
|
public String get(Language langEnum, String message) throws IOException, InterruptedException {
|
||||||
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
||||||
String lang = langEnum.toString();
|
|
||||||
if (langEnum == Language.EN_US) {
|
|
||||||
lang = "EN-US";
|
|
||||||
} else if (langEnum == Language.EN_GB) {
|
|
||||||
lang = "EN-GB";
|
|
||||||
} else if (langEnum == Language.PT_BR) {
|
|
||||||
lang = "PT-BR";
|
|
||||||
} else if (langEnum == Language.PT_PT) {
|
|
||||||
lang = "PT-PT";
|
|
||||||
}
|
|
||||||
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
|
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
HttpRequest request = HttpRequest.newBuilder().GET()
|
HttpRequest request = HttpRequest.newBuilder().GET()
|
||||||
.header("Accept","*/*")
|
.header("Accept","*/*")
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||||
.uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key=" + encodedAuthKey + "&target_lang=" + lang + "&text=" + encodedMessage)).build();
|
.uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key=" + encodedAuthKey + "&target_lang=" + langEnum.getApiString() + "&text=" + encodedMessage)).build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
return response.body();
|
return response.body();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Language {
|
public enum Language {
|
||||||
BG,
|
Bulgarian("BG"),
|
||||||
CS,
|
Czech("CS"),
|
||||||
DA,
|
Danish("DA"),
|
||||||
DE,
|
German("DE"),
|
||||||
EL,
|
Greek("EL"),
|
||||||
EN_GB,
|
EnglishUK("EN-GB"),
|
||||||
EN_US,
|
EnglishUS("EN-US"),
|
||||||
ES,
|
Spanish("ES"),
|
||||||
ET,
|
Estonian("ET"),
|
||||||
FI,
|
Finish("FI"),
|
||||||
FR,
|
French("FR"),
|
||||||
HU,
|
Hungarian("HU"),
|
||||||
IT,
|
Italian("IT"),
|
||||||
JA,
|
Japanese("JA"),
|
||||||
LT,
|
Lithuanian("LT"),
|
||||||
LV,
|
Latvian("LV"),
|
||||||
NL,
|
Dutch("NL"),
|
||||||
PL,
|
Polish("PL"),
|
||||||
PT_BR,
|
PortugueseBrazil("PT-BR"),
|
||||||
PT_PT,
|
PortuguesePortugal("PT-PT"),
|
||||||
RO,
|
Romanian("RO"),
|
||||||
RU,
|
Russian("RU"),
|
||||||
SK,
|
Slovak("SK"),
|
||||||
SL,
|
Slovenian("SL"),
|
||||||
SV,
|
Swedish("SV"),
|
||||||
ZH
|
Chinese("ZH");
|
||||||
|
private final String apiString;
|
||||||
|
Language(String apiString) {
|
||||||
|
this.apiString = apiString;
|
||||||
|
}
|
||||||
|
public String getApiString() {
|
||||||
|
return this.apiString;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
|
|
||||||
// ███████╗ ██████╗ ██╗ ██╗ ██╗██████╗ ██████╗ █████╗ ████████╗███████╗██████╗ ██████╗ ███████╗███████╗██████╗ ██╗ //
|
|
||||||
// ██╔════╝██╔════╝ ██║ ██║ ██║██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔══██╗ ██╔══██╗██╔════╝██╔════╝██╔══██╗ ██║ //
|
|
||||||
// ███████╗██║ ███╗██║ ██║ ██║██████╔╝██║ ██║███████║ ██║ █████╗ ██║ ██║ ██║ ██║█████╗ █████╗ ██████╔╝ ██║ //
|
|
||||||
// ╚════██║██║ ██║██║ ██║ ██║██╔═══╝ ██║ ██║██╔══██║ ██║ ██╔══╝ ██║ ██║ ██║ ██║██╔══╝ ██╔══╝ ██╔═══╝██ ██║ //
|
|
||||||
// ███████║╚██████╔╝██║ ╚██████╔╝██║ ██████╔╝██║ ██║ ██║ ███████╗██████╔╝ ██████╔╝███████╗███████╗██║ ╚█████╔╝ //
|
|
||||||
// ╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═════╝ ╚═════╝ ╚══════╝╚══════╝╚═╝ ╚════╝ //
|
|
||||||
// █▀█ █▀█ █ █▀▀ █ █▄ █ ▄▀█ █ █▄▄ █▄█ █▀ ▀█▀ █▀▀ █ █ █▀▀ █▄ █ //
|
|
||||||
// █▄█ █▀▄ █ █▄█ █ █ ▀█ █▀█ █▄▄ █▄█ █ ▄█ █ ██▄ ▀▄▀ ██▄ █ ▀█ //
|
|
||||||
// //
|
|
||||||
// DeepJ is a Java Wrapper for the DeepL Free Public API. This was originally made by Steven (Forked From) and Updated by 5gi //
|
|
||||||
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
|
|
Loading…
Reference in New Issue
Block a user