forked from 5gi/DeepJ
Update 'src/main/java/tech/nevets/deepj/Translator.java'
This commit is contained in:
parent
8c39329d6b
commit
b9fb097836
@ -9,24 +9,51 @@ import java.net.http.HttpRequest;
|
|||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
public class Translator {
|
public class Translator {
|
||||||
Request request;
|
//TODO TEST CLASS
|
||||||
public Translator(String authKey) {
|
public static String globalAuthKey = "null";
|
||||||
|
|
||||||
|
public static void setGlobalAuthKey(String key) {
|
||||||
|
this.globalAuthKey = key;
|
||||||
|
}
|
||||||
|
private 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(@NotNull String authKey) {
|
||||||
|
if (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);
|
request = new Request(authKey);
|
||||||
}
|
}
|
||||||
|
public Translator(@NotNull String authKey, @NotNull boolean silence) {
|
||||||
|
if (globalAuthKey != "null" && silence == false) {
|
||||||
|
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 String translate(Language langToTranslateTo, String sourceMessage) {
|
public String translate(Language langToTranslateTo, String sourceMessage) {
|
||||||
String response = "Error processing request";
|
String response = "Error processing request";
|
||||||
try {
|
try {
|
||||||
response = request.get(langToTranslateTo, sourceMessage);
|
response = request.queryAPI(langToTranslateTo, sourceMessage);
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
System.out.println("Error processing request");
|
System.out.println(response);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
String message = "Error Processing Request";
|
String message = "Error Processing Request";
|
||||||
JSONObject jo = new JSONObject(response);
|
JSONObject jsonObject = new JSONObject(response);
|
||||||
JSONArray ja = jo.getJSONArray("translations");
|
JSONArray jsonArray = jsonObject.getJSONArray("translations");
|
||||||
for (int i = 0; i < ja.length(); i++) {
|
for (int i = 0; i < jsonArray.length(); i++) {
|
||||||
JSONObject joo = ja.getJSONObject(i);
|
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
|
||||||
message = joo.getString("text");
|
message = jsonObject.getString("text");
|
||||||
}
|
}
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
@ -35,7 +62,7 @@ public class Translator {
|
|||||||
public Request(String authKey) {
|
public Request(String authKey) {
|
||||||
this.authKey = authKey;
|
this.authKey = authKey;
|
||||||
}
|
}
|
||||||
public String get(Language langEnum, String message) throws IOException, InterruptedException {
|
public String queryAPI(Language langEnum, String message) throws IOException, InterruptedException {
|
||||||
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();
|
||||||
|
Loading…
Reference in New Issue
Block a user