forked from 5gi/DeepJ
137 lines
5.7 KiB
Java
137 lines
5.7 KiB
Java
package com.the5gi.DeepJ;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
import java.io.IOException;
|
|
import java.net.URI;
|
|
import java.net.URLEncoder;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpResponse;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Objects;
|
|
public class Translator {
|
|
public static String globalAuthKey = "null";
|
|
protected Request request;
|
|
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");
|
|
} 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 :)");
|
|
}
|
|
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 :)");
|
|
}
|
|
request = new Request(authKey);
|
|
}
|
|
public static void setGlobalAuthKey(String key) {
|
|
globalAuthKey = key;
|
|
}
|
|
public static Translator newTranslator(String authKey) {
|
|
return new Translator(authKey);
|
|
}
|
|
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");
|
|
return new Translator("null");
|
|
} else {
|
|
return new Translator();
|
|
}
|
|
}
|
|
public void setAuthKey(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");
|
|
}
|
|
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);
|
|
} catch (IOException | InterruptedException e) {
|
|
System.out.println(response);
|
|
e.printStackTrace();
|
|
}
|
|
String message = "Error Processing Request (Note: This could be due to the api updating or your api being taxed to often)";
|
|
JSONObject jsonObject = new JSONObject(response);
|
|
JSONArray jsonArray = jsonObject.getJSONArray("translations");
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
|
|
message = jsonObject1.getString("text");
|
|
}
|
|
return message;
|
|
}
|
|
protected static class Request {
|
|
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());
|
|
return response.body();
|
|
}
|
|
}
|
|
public enum Language {
|
|
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;
|
|
}
|
|
}
|
|
}
|