Fixed repetitive unreadable enum key.

Made code less if statementy
This commit is contained in:
5gi 2022-11-03 19:01:38 -04:00
parent 2c276acaf9
commit 7958399f0c
2 changed files with 49 additions and 66 deletions

View File

@ -1,3 +1,13 @@
# 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 |
|:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::|

View File

@ -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;
import org.json.JSONArray;
import org.json.JSONObject;
@ -25,7 +13,7 @@ public class Translator {
public Translator(String 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";
try {
response = request.get(langToTranslateTo, sourceMessage);
@ -47,67 +35,52 @@ public class Translator {
public Request(String 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 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);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().GET()
.header("Accept","*/*")
.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());
return response.body();
}
}
public enum Language {
BG,
CS,
DA,
DE,
EL,
EN_GB,
EN_US,
ES,
ET,
FI,
FR,
HU,
IT,
JA,
LT,
LV,
NL,
PL,
PT_BR,
PT_PT,
RO,
RU,
SK,
SL,
SV,
ZH
Bulgarian("BG"),
Czech("CS"),
Danish("DA"),
German("DE"),
Greek("EL"),
EnglishUK("EN-GB"),
EnglishUS("EN-US"),
Spanish("ES"),
Estonian("ET"),
Finish("FI"),
French("FR"),
Hungarian("HU"),
Italian("IT"),
Japanese("JA"),
Lithuanian("LT"),
Latvian("LV"),
Dutch("NL"),
Polish("PL"),
PortugueseBrazil("PT-BR"),
PortuguesePortugal("PT-PT"),
Romanian("RO"),
Russian("RU"),
Slovak("SK"),
Slovenian("SL"),
Swedish("SV"),
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 //
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://
}