|
|
|
|
@@ -11,54 +11,88 @@ import java.net.http.HttpResponse;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
public class Translator {
|
|
|
|
|
public static String globalAuthKey = "null";
|
|
|
|
|
public static void setGlobalAuthKey(String key) {
|
|
|
|
|
globalAuthKey = key;
|
|
|
|
|
}
|
|
|
|
|
private Request request;
|
|
|
|
|
|
|
|
|
|
//REQUEST INSIDE OF TRANSLATOR
|
|
|
|
|
protected Request request;
|
|
|
|
|
|
|
|
|
|
//GLOBAL AUTH KEY (GAK)
|
|
|
|
|
public static String globalAPIKey = "null";
|
|
|
|
|
|
|
|
|
|
//CONSTRUCTORS
|
|
|
|
|
|
|
|
|
|
public Translator() {
|
|
|
|
|
if (globalAuthKey == null) {
|
|
|
|
|
/*
|
|
|
|
|
[DeepJ] You are currently using the Global Auth Key Translator Constructor.
|
|
|
|
|
[DeepJ] If you are trying to use global auth keys, Call the method "Translator.setGlobalAuthKey(String key)" and then re-use this contructor.
|
|
|
|
|
[DeepJ] If you want to define a different authkey every time do: "new Translator(String authKey)"
|
|
|
|
|
*/
|
|
|
|
|
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");
|
|
|
|
|
//GAK
|
|
|
|
|
if (globalAPIKey == null) {
|
|
|
|
|
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 constructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
|
|
|
|
|
} else {
|
|
|
|
|
request = new Request(globalAuthKey);
|
|
|
|
|
request = new Request(globalAPIKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public Translator(boolean silent) {
|
|
|
|
|
//GAK (SILENCABLE)
|
|
|
|
|
if (globalAPIKey == null && !silent) {
|
|
|
|
|
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 constructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
|
|
|
|
|
} else {
|
|
|
|
|
request = new Request(globalAPIKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public Translator(String authKey) {
|
|
|
|
|
if (!Objects.equals(globalAuthKey, "null")) {
|
|
|
|
|
/*
|
|
|
|
|
[DeepJ] Looks like you have defined a global auth key already! You can use if by just typing "new Traslator()"
|
|
|
|
|
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 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 :)");
|
|
|
|
|
//NORMAL REQUEST (GAK Notice) (NO SILENCE)
|
|
|
|
|
if (!Objects.equals(globalAPIKey, "null")) {
|
|
|
|
|
System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Translator()\"" + "\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) {
|
|
|
|
|
/*
|
|
|
|
|
[DeepJ] Looks like you have defined a global auth key already! You can use if by just typing "new Traslator()"
|
|
|
|
|
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 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 :)");
|
|
|
|
|
public Translator(String apiKey, boolean silence) {
|
|
|
|
|
//NORMAL REQUEST (GAK SILENCEABLE)
|
|
|
|
|
if (!Objects.equals(globalAPIKey, "null") && !silence) {
|
|
|
|
|
System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Translator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but this is just some advice :)");
|
|
|
|
|
}
|
|
|
|
|
request = new Request(authKey);
|
|
|
|
|
request = new Request(apiKey);
|
|
|
|
|
}
|
|
|
|
|
public void setAPIKey(String authKey) {
|
|
|
|
|
request = new Request(authKey);
|
|
|
|
|
public static void setGlobalAPIKey(String key) {
|
|
|
|
|
//SET GLOBAL API KEY (STATIC)
|
|
|
|
|
globalAPIKey = key;
|
|
|
|
|
}
|
|
|
|
|
public static Translator newTranslator(String apiKey) {
|
|
|
|
|
//NORMAL REQUEST (NO GAK) (STATIC)
|
|
|
|
|
return new Translator(apiKey);
|
|
|
|
|
}
|
|
|
|
|
public static TranslatorBuilder newBuilder() {
|
|
|
|
|
return new TranslatorBuilder();
|
|
|
|
|
}
|
|
|
|
|
public static Translator newTranslator() {
|
|
|
|
|
//GAK (STATIC)
|
|
|
|
|
if (globalAPIKey == null) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//END CONSTRUCTORS
|
|
|
|
|
|
|
|
|
|
//API KEYS
|
|
|
|
|
public void setAPIKey(String key) {
|
|
|
|
|
request.setAuthKey(key);
|
|
|
|
|
}
|
|
|
|
|
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!");
|
|
|
|
|
return "null";
|
|
|
|
|
}
|
|
|
|
|
String response = "Error processing request";
|
|
|
|
|
try {
|
|
|
|
|
response = request.queryAPI(langToTranslateTo, sourceMessage);
|
|
|
|
|
@@ -75,23 +109,62 @@ instead of "new Translator(String authKey)". NOTE: This will work but just some
|
|
|
|
|
}
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//API REQUEST CLASS
|
|
|
|
|
protected static class Request {
|
|
|
|
|
public final String authKey;
|
|
|
|
|
public String authKey;
|
|
|
|
|
public Request(String authKey) {
|
|
|
|
|
this.authKey = authKey;
|
|
|
|
|
}
|
|
|
|
|
public void setAuthKey(String key) {
|
|
|
|
|
authKey = key;
|
|
|
|
|
}
|
|
|
|
|
public String queryAPI(Language langEnum, String message) throws IOException, InterruptedException {
|
|
|
|
|
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
|
|
|
|
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=" + langEnum.getApiString() + "&text=" + encodedMessage)).build();
|
|
|
|
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
URI deepLURI = URI.create("https://api-free.deepl.com/v2/translate?auth_key="+encodedAuthKey+"&target_lang="+langEnum.getApiString()+"&text="+encodedMessage);
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
.GET()
|
|
|
|
|
.header("Accept","*/*")
|
|
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
.uri(deepLURI)
|
|
|
|
|
.build();
|
|
|
|
|
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
return response.body();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class TranslatorBuilder {
|
|
|
|
|
private boolean silent = false;
|
|
|
|
|
private String authKey = null;
|
|
|
|
|
private boolean GAK = false;
|
|
|
|
|
|
|
|
|
|
public Translator build() {
|
|
|
|
|
if (GAK) {
|
|
|
|
|
return new Translator(silent);
|
|
|
|
|
} else {
|
|
|
|
|
return new Translator(authKey, silent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TranslatorBuilder useGAK(boolean bool) {
|
|
|
|
|
this.GAK = bool;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TranslatorBuilder setAuthKey(String authKey) {
|
|
|
|
|
this.authKey = authKey;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TranslatorBuilder setSilent(boolean silent) {
|
|
|
|
|
this.silent = silent;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//DEEPL LANGUAGES
|
|
|
|
|
public enum Language {
|
|
|
|
|
Bulgarian("BG"),
|
|
|
|
|
Czech("CS"),
|
|
|
|
|
@@ -119,12 +192,16 @@ instead of "new Translator(String authKey)". NOTE: This will work but just some
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|