change lib

This commit is contained in:
Thomas 2023-02-14 10:45:23 +01:00
parent 5e4298c566
commit 4eb4c6eea5
4 changed files with 64 additions and 6 deletions

View File

@ -49,6 +49,8 @@ public class TranslateHelper {
MyTransL.translatorEngine et;
if (translator.compareToIgnoreCase("FEDILAB") == 0) {
et = MyTransL.translatorEngine.LIBRETRANSLATE;
} else if (translator.compareToIgnoreCase("LINGVA") == 0) {
et = MyTransL.translatorEngine.LINGVA;
} else {
et = MyTransL.translatorEngine.DEEPL;
}
@ -60,6 +62,8 @@ public class TranslateHelper {
params.setSource_lang("auto");
if (translator.compareToIgnoreCase("FEDILAB") == 0) {
myTransL.setLibretranslateDomain("translate.fedilab.app");
} else if (translator.compareToIgnoreCase("LINGVA") == 0) {
myTransL.setLibretranslateDomain("lingva.ml");
} else {
String translatorVersion = sharedpreferences.getString(context.getString(R.string.SET_TRANSLATOR_VERSION), "PRO");
params.setPro(translatorVersion.equals("PRO"));

View File

@ -28,8 +28,9 @@ public class MyTransL {
public static String TAG = "MyTrans_TAG";
private static MyTransL myTransL;
private static String libretranslateDomain;
private static String lingvaDomain;
private final translatorEngine te;
private String yandexAPIKey, deeplAPIKey, systranAPIKey, libreTranslateAPIKey;
private String yandexAPIKey, deeplAPIKey, systranAPIKey, libreTranslateAPIKey, lingvaAPIKey;
private int timeout = 30;
private boolean obfuscation = false;
@ -44,7 +45,7 @@ public class MyTransL {
}
/**
* Allows to get the current locale of the device
* Allows to get the current domain for libre translate
*
* @return locale String
*/
@ -52,6 +53,16 @@ public class MyTransL {
return "https://" + libretranslateDomain + "/translate?";
}
/**
* Allows to get the current domain for lingva
*
* @return locale String
*/
public static String getLingvaUrl() {
return "https://" + lingvaDomain + "/api/v1/";
}
/**
* Allows to get the current locale of the device
*
@ -110,6 +121,15 @@ public class MyTransL {
MyTransL.libretranslateDomain = libretranslateDomain;
}
public String getingvaDomain() {
return lingvaDomain;
}
public void setLingvaDomain(String lingvaDomain) {
MyTransL.lingvaDomain = lingvaDomain;
}
public String getLibreTranslateAPIKey() {
return libreTranslateAPIKey;
}
@ -118,6 +138,14 @@ public class MyTransL {
this.libreTranslateAPIKey = libreTranslateAPIKey;
}
public String getLingvaAPIKey() {
return lingvaAPIKey;
}
public void setLingvaAPIKey(String lingvaAPIKey) {
this.lingvaAPIKey = lingvaAPIKey;
}
/**
* Asynchronous call for the translation
*
@ -144,7 +172,8 @@ public class MyTransL {
YANDEX,
DEEPL,
SYSTRAN,
LIBRETRANSLATE
LIBRETRANSLATE,
LINGVA
}
}

View File

@ -146,6 +146,10 @@ public class TransAsync {
e.printStackTrace();
}
str_response = new Client().post(MyTransL.getLibreTranslateUrl(), this.timeout, params);
} else if (te == MyTransL.translatorEngine.LINGVA) {
String key = MyTransL.getInstance(te).getLibreTranslateAPIKey();
String lingvaURL = MyTransL.getLingvaUrl() + this.params.getSource_lang() + "/" + toLanguage + "/" + contentToSend;
str_response = new Client().get(lingvaURL, this.timeout);
}
} catch (IOException | NoSuchAlgorithmException | KeyManagementException err) {
this.e = new HttpsConnectionException(-1, err.getMessage());
@ -167,6 +171,8 @@ public class TransAsync {
translate.parseSystranlResult(result, listener);
} else if (this.te == MyTransL.translatorEngine.LIBRETRANSLATE) {
translate.parseLibreTranslateResult(result, listener);
} else if (this.te == MyTransL.translatorEngine.LINGVA) {
translate.parseLingvaResult(result, listener);
}
//Obfuscation if asked
if (obfuscation) {

View File

@ -318,13 +318,12 @@ public class Translate {
}
/***
* Method to parse result coming from the Deepl translator
* More about Deepl translate API - https://www.deepl.com/api-reference.html
* Method to parse result coming from the Libre Translate
* @param response String - Response of the engine translator
* @param listener - Results Listener
*/
public void parseLibreTranslateResult(String response, Results listener) {
translate.setTranslatorEngine(MyTransL.translatorEngine.DEEPL);
translate.setTranslatorEngine(MyTransL.translatorEngine.LIBRETRANSLATE);
try {
JSONObject translationJson = new JSONObject(response);
//Retrieves the translated content
@ -339,6 +338,26 @@ public class Translate {
}
/***
* Method to parse result coming from the Lingva
* @param response String - Response of the engine translator
* @param listener - Results Listener
*/
public void parseLingvaResult(String response, Results listener) {
translate.setTranslatorEngine(MyTransL.translatorEngine.LINGVA);
try {
JSONObject translationJson = new JSONObject(response);
//Retrieves the translated content
translate.setTranslatedContent(translationJson.getString("translation"));
//Retrieves the initial language
translate.setInitialLanguage(initialLanguage);
} catch (JSONException e1) {
e1.printStackTrace();
HttpsConnectionException httpsConnectionException = new HttpsConnectionException(-1, e1.getMessage());
listener.onFail(httpsConnectionException);
}
}
/***
* Method to parse result coming from the Deepl translator
* More about Deepl translate API - https://www.deepl.com/api-reference.html