Update 'src/main/java/com/the5gi/DeepJ/Translator.java'

This commit is contained in:
5gi 2022-12-07 11:17:24 -05:00
parent d8606a0587
commit b12e497afa

View File

@ -11,28 +11,34 @@ import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Objects; import java.util.Objects;
public class Translator { public class Translator {
public static String globalAuthKey = "null";
//REQUEST INSIDE OF TRANSLATOR
protected Request request; protected Request request;
//GLOBAL AUTH KEY
public static String globalAPIKey = "null";
//CONSTRUCTORS
public Translator() { public Translator() {
if (globalAuthKey == null) { if (globalAuthKey == null) {
System.out.println("\n[DeepJ] You are currently using the Global Auth Key Translator Constructor." +"\n[DeepJ] If you are trying to use global auth keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this contructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n"); System.out.println("\n[DeepJ] You are currently using the Global API Key Translator Constructor." +"\n[DeepJ] If you are trying to use global API keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this contructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
} else { } else {
request = new Request(globalAuthKey); request = new Request(globalAuthKey);
} }
} }
public Translator(String authKey) { public Translator(String authKey) {
if (!Objects.equals(globalAuthKey, "null")) { if (!Objects.equals(globalAuthKey, "null")) {
System.out.println("[DeepJ] Looks like you have defined a global auth key already! You can use if by just typing \"new Traslator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but just some advice :)"); System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Traslator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but just some advice :)");
} }
request = new Request(authKey); request = new Request(authKey);
} }
public Translator(String authKey, boolean silence) { public Translator(String authKey, boolean silence) {
if (!Objects.equals(globalAuthKey, "null") && !silence) { if (!Objects.equals(globalAuthKey, "null") && !silence) {
System.out.println("[DeepJ] Looks like you have defined a global auth key already! You can use if by just typing \"new Traslator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but this is just some advice :)"); System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Traslator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but this is just some advice :)");
} }
request = new Request(authKey); request = new Request(authKey);
} }
public static void setGlobalAuthKey(String key) { public static void setGlobalAPIKey(String key) {
globalAuthKey = key; globalAuthKey = key;
} }
public static Translator newTranslator(String authKey) { public static Translator newTranslator(String authKey) {
@ -40,26 +46,30 @@ public class Translator {
} }
public static Translator newTranslator() { public static Translator newTranslator() {
if (globalAuthKey == null) { if (globalAuthKey == null) {
System.out.println("\n[DeepJ] You are currently using the Global Auth Key \".of()\" Translator." + System.out.println("\n[DeepJ] You are currently using the Global API Key \".of()\" Translator." +
"\n[DeepJ] If you are trying to use global auth keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this method." + "\n[DeepJ] If you are trying to use global API keys, Call the method \"Translator.setGlobalAPIKey(String key)\" and then re-use this method." +
"\n[DeepJ] If you want to define an different authkey every time do: \"Translator.of(String authKey)\"\n"); "\n[DeepJ] If you want to define an different authkey every time do: \"Translator.of(String apiKey)\"\n");
return new Translator("null"); return new Translator("null");
} else { } else {
return new Translator(); return new Translator();
} }
} }
public void setAuthKey(String key) { //END CONSTRUCTORS
//API KEYS
public void setAPIKey(String key) {
request.setAuthKey(key); request.setAuthKey(key);
} }
public void setAPIKey(String authKey) {
request = new Request(authKey);
}
public String getAPIKey() { public String getAPIKey() {
return request.authKey; return request.authKey;
} }
public void close() { public void close() {
request = new Request("null"); request = new Request("null");
} }
//END API KEYS
//TRANSLATE
public String translate(Language langToTranslateTo, String sourceMessage) { public String translate(Language langToTranslateTo, String sourceMessage) {
if (request.authKey == "null") { if (request.authKey == "null") {
System.out.println("[DeepJ] This translator is closed! Please re-create!"); System.out.println("[DeepJ] This translator is closed! Please re-create!");
@ -81,6 +91,8 @@ public class Translator {
} }
return message; return message;
} }
//API REQUEST CLASS
protected static class Request { protected static class Request {
public String authKey; public String authKey;
public Request(String authKey) { public Request(String authKey) {
@ -98,6 +110,8 @@ public class Translator {
return response.body(); return response.body();
} }
} }
//DEEPL LANGUAGES
public enum Language { public enum Language {
Bulgarian("BG"), Bulgarian("BG"),
Czech("CS"), Czech("CS"),
@ -125,10 +139,14 @@ public class Translator {
Slovenian("SL"), Slovenian("SL"),
Swedish("SV"), Swedish("SV"),
Chinese("ZH"); Chinese("ZH");
//API STRING IDENTIFIER
private final String apiString; private final String apiString;
Language(String apiString) { Language(String apiString) {
this.apiString = apiString; this.apiString = apiString;
} }
//API STRING IDENTIFIER RETURN
public String getApiString() { public String getApiString() {
return this.apiString; return this.apiString;
} }