diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/TranslateHelper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/TranslateHelper.java index e347044eb..1d514ba9b 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/TranslateHelper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/TranslateHelper.java @@ -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")); diff --git a/mytransl/src/main/java/com/github/stom79/mytransl/MyTransL.java b/mytransl/src/main/java/com/github/stom79/mytransl/MyTransL.java index 4f96bc5c0..e2c997ec7 100644 --- a/mytransl/src/main/java/com/github/stom79/mytransl/MyTransL.java +++ b/mytransl/src/main/java/com/github/stom79/mytransl/MyTransL.java @@ -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 } } diff --git a/mytransl/src/main/java/com/github/stom79/mytransl/async/TransAsync.java b/mytransl/src/main/java/com/github/stom79/mytransl/async/TransAsync.java index 7c9052199..c3654aa45 100644 --- a/mytransl/src/main/java/com/github/stom79/mytransl/async/TransAsync.java +++ b/mytransl/src/main/java/com/github/stom79/mytransl/async/TransAsync.java @@ -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) { diff --git a/mytransl/src/main/java/com/github/stom79/mytransl/translate/Translate.java b/mytransl/src/main/java/com/github/stom79/mytransl/translate/Translate.java index 6bccabff0..38997b1f4 100644 --- a/mytransl/src/main/java/com/github/stom79/mytransl/translate/Translate.java +++ b/mytransl/src/main/java/com/github/stom79/mytransl/translate/Translate.java @@ -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