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.util.Objects;
public class Translator {
public static String globalAuthKey = "null";
//REQUEST INSIDE OF TRANSLATOR
protected Request request;
//GLOBAL AUTH KEY
public static String globalAPIKey = "null";
//CONSTRUCTORS
public Translator() {
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 {
request = new Request(globalAuthKey);
}
}
public Translator(String authKey) {
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);
}
public Translator(String authKey, boolean 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);
}
public static void setGlobalAuthKey(String key) {
public static void setGlobalAPIKey(String key) {
globalAuthKey = key;
}
public static Translator newTranslator(String authKey) {
@ -40,26 +46,30 @@ public class Translator {
}
public static Translator newTranslator() {
if (globalAuthKey == null) {
System.out.println("\n[DeepJ] You are currently using the Global Auth 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 want to define an different authkey every time do: \"Translator.of(String authKey)\"\n");
System.out.println("\n[DeepJ] You are currently using the Global API Key \".of()\" Translator." +
"\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 apiKey)\"\n");
return new Translator("null");
} else {
return new Translator();
}
}
public void setAuthKey(String key) {
//END CONSTRUCTORS
//API KEYS
public void setAPIKey(String key) {
request.setAuthKey(key);
}
public void setAPIKey(String authKey) {
request = new Request(authKey);
}
public String getAPIKey() {
return request.authKey;
}
public void close() {
request = new Request("null");
}
//END API KEYS
//TRANSLATE
public String translate(Language langToTranslateTo, String sourceMessage) {
if (request.authKey == "null") {
System.out.println("[DeepJ] This translator is closed! Please re-create!");
@ -81,6 +91,8 @@ public class Translator {
}
return message;
}
//API REQUEST CLASS
protected static class Request {
public String authKey;
public Request(String authKey) {
@ -98,6 +110,8 @@ public class Translator {
return response.body();
}
}
//DEEPL LANGUAGES
public enum Language {
Bulgarian("BG"),
Czech("CS"),
@ -125,10 +139,14 @@ public class Translator {
Slovenian("SL"),
Swedish("SV"),
Chinese("ZH");
//API STRING IDENTIFIER
private final String apiString;
Language(String apiString) {
this.apiString = apiString;
}
//API STRING IDENTIFIER RETURN
public String getApiString() {
return this.apiString;
}