forked from Steven/DeepJ
Compare commits
No commits in common. "master" and "master" have entirely different histories.
@ -4,7 +4,7 @@
|
|||||||
<component name="FrameworkDetectionExcludesConfiguration">
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
<file type="web" url="file://$PROJECT_DIR$" />
|
<file type="web" url="file://$PROJECT_DIR$" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="temurin-11" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -5,7 +5,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
apply plugin: 'maven-publish'
|
apply plugin: 'maven-publish'
|
||||||
group 'com.the5gi.deepj'
|
group 'com.the5gi.deepj'
|
||||||
version '1.4.3'
|
version '1.3.7'
|
||||||
|
|
||||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
||||||
|
|
||||||
|
@ -11,83 +11,55 @@ 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 (GAK)
|
|
||||||
public static String globalAPIKey = "null";
|
|
||||||
|
|
||||||
//CONSTRUCTORS
|
|
||||||
|
|
||||||
public Translator() {
|
public Translator() {
|
||||||
//GAK
|
if (globalAuthKey == null) {
|
||||||
if (globalAPIKey == 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 constructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
|
|
||||||
} else {
|
} else {
|
||||||
request = new Request(globalAPIKey);
|
request = new Request(globalAuthKey);
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
public Translator(String authKey) {
|
||||||
//NORMAL REQUEST (GAK Notice) (NO SILENCE)
|
if (!Objects.equals(globalAuthKey, "null")) {
|
||||||
if (!Objects.equals(globalAPIKey, "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 Translator()\"" + "\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 apiKey, boolean silence) {
|
public Translator(String authKey, boolean silence) {
|
||||||
//NORMAL REQUEST (GAK SILENCEABLE)
|
if (!Objects.equals(globalAuthKey, "null") && !silence) {
|
||||||
if (!Objects.equals(globalAPIKey, "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 Translator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but this is just some advice :)");
|
|
||||||
}
|
}
|
||||||
request = new Request(apiKey);
|
request = new Request(authKey);
|
||||||
}
|
}
|
||||||
public static void setGlobalAPIKey(String key) {
|
public static void setGlobalAuthKey(String key) {
|
||||||
//SET GLOBAL API KEY (STATIC)
|
globalAuthKey = key;
|
||||||
globalAPIKey = key;
|
|
||||||
}
|
}
|
||||||
public static Translator newTranslator(String apiKey) {
|
public static Translator newTranslator(String authKey) {
|
||||||
//NORMAL REQUEST (NO GAK) (STATIC)
|
return new Translator(authKey);
|
||||||
return new Translator(apiKey);
|
|
||||||
}
|
|
||||||
public static TranslatorBuilder newBuilder() {
|
|
||||||
return new TranslatorBuilder();
|
|
||||||
}
|
}
|
||||||
public static Translator newTranslator() {
|
public static Translator newTranslator() {
|
||||||
//GAK (STATIC)
|
if (globalAuthKey == null) {
|
||||||
if (globalAPIKey == 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//END CONSTRUCTORS
|
public void setAuthKey(String key) {
|
||||||
|
|
||||||
//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!");
|
||||||
@ -109,8 +81,6 @@ 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) {
|
||||||
@ -123,48 +93,11 @@ public class Translator {
|
|||||||
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
||||||
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
|
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
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(URI.create("https://api-free.deepl.com/v2/translate?auth_key="+encodedAuthKey+"&target_lang="+langEnum.getApiString()+"&text="+encodedMessage)).build();
|
||||||
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());
|
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
|
||||||
return response.body();
|
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 {
|
public enum Language {
|
||||||
Bulgarian("BG"),
|
Bulgarian("BG"),
|
||||||
Czech("CS"),
|
Czech("CS"),
|
||||||
@ -192,14 +125,10 @@ 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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user