From 255eeac0a5c048a12467bc88f7d7b374333f4a8c Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 17 Jan 2023 18:08:10 +0100 Subject: [PATCH] - Add several targeted languages for translator --- .../android/helper/TranslateHelper.java | 80 +++++++++++++++---- app/src/main/res/values/strings.xml | 5 +- app/src/main/res/xml/pref_timelines.xml | 5 +- .../metadata/android/en/changelogs/469.txt | 1 + 4 files changed, 70 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/helper/TranslateHelper.java b/app/src/main/java/app/fedilab/android/helper/TranslateHelper.java index 2876feea..41266be9 100644 --- a/app/src/main/java/app/fedilab/android/helper/TranslateHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/TranslateHelper.java @@ -20,14 +20,20 @@ import android.os.Build; import android.text.Html; import android.widget.Toast; +import androidx.appcompat.app.AlertDialog; import androidx.preference.PreferenceManager; import com.github.stom79.mytransl.MyTransL; import com.github.stom79.mytransl.client.HttpsConnectionException; import com.github.stom79.mytransl.client.Results; import com.github.stom79.mytransl.translate.Params; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import java.util.List; +import java.util.Set; import app.fedilab.android.R; +import app.fedilab.android.client.entities.app.Languages; import es.dmoral.toasty.Toasty; public class TranslateHelper { @@ -63,28 +69,72 @@ public class TranslateHelper { } } - String translate = sharedpreferences.getString(context.getString(R.string.SET_LIVE_TRANSLATE), MyTransL.getLocale()); - if (translate.equalsIgnoreCase("default")) { + Set translates = sharedpreferences.getStringSet(context.getString(R.string.SET_LIVE_TRANSLATE_MULTIPLE), null); + String translate; + if (translates == null || translates.size() == 0) { translate = MyTransL.getLocale(); - } + myTransL.translate(statusToTranslate, translate, params, new Results() { + @Override + public void onSuccess(com.github.stom79.mytransl.translate.Translate translate) { + if (translate.getTranslatedContent() != null) { + callback.onTranslate(translate.getTranslatedContent()); + } else { + callback.onTranslate(""); + Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); + } + } - myTransL.translate(statusToTranslate, translate, params, new Results() { - @Override - public void onSuccess(com.github.stom79.mytransl.translate.Translate translate) { - if (translate.getTranslatedContent() != null) { - callback.onTranslate(translate.getTranslatedContent()); - } else { + @Override + public void onFail(HttpsConnectionException httpsConnectionException) { callback.onTranslate(""); Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); } + }); + } else { + String[] codesArr = new String[translates.size()]; + String[] languagesArr = new String[translates.size()]; + int j = 0; + List languages = Languages.get(context); + if (languages == null) { + return; } + for (String val : translates) { + codesArr[j] = val; + for (Languages.Language language : languages) { + if (language.code.trim().equalsIgnoreCase(val.trim())) { + languagesArr[j] = language.language; + break; + } + } + j++; + } + AlertDialog.Builder builder = new MaterialAlertDialogBuilder(context, Helper.dialogStyle()); + builder.setTitle(context.getString(R.string.translate_in)); + builder.setItems(languagesArr, (dialogInterface, i) -> { + myTransL.translate(statusToTranslate, codesArr[i], params, new Results() { + @Override + public void onSuccess(com.github.stom79.mytransl.translate.Translate translate) { + if (translate.getTranslatedContent() != null) { + callback.onTranslate(translate.getTranslatedContent()); + } else { + callback.onTranslate(""); + Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onFail(HttpsConnectionException httpsConnectionException) { + callback.onTranslate(""); + Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); + } + }); + dialogInterface.dismiss(); + }); + builder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); + builder.create().show(); + } + - @Override - public void onFail(HttpsConnectionException httpsConnectionException) { - callback.onTranslate(""); - Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); - } - }); } public interface Translate { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 58ed3f43..c13598d0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1267,7 +1267,6 @@ isiZulu - default en fr de @@ -1303,7 +1302,6 @@ vi - ----- English Français Deutsch @@ -1482,7 +1480,7 @@ SET_GROUP_REBLOGS SET_BOOST_ORIGINAL_DATE SET_HIDE_SINGLE_MEDIA_WITH_CARD - SET_LIVE_TRANSLATE + SET_LIVE_TRANSLATE_MULTIPLE SET_TRUNCATE_TOOTS_SIZE SET_ART_WITH_NSFW SET_VIDEO_CACHE @@ -2229,4 +2227,5 @@ Hide single media when there is a link preview Change subscribed languages Filter languages + Translate in \ No newline at end of file diff --git a/app/src/main/res/xml/pref_timelines.xml b/app/src/main/res/xml/pref_timelines.xml index f2ceb3f2..f99027d2 100644 --- a/app/src/main/res/xml/pref_timelines.xml +++ b/app/src/main/res/xml/pref_timelines.xml @@ -38,12 +38,11 @@ app:key="@string/SET_HIDE_SINGLE_MEDIA_WITH_CARD" app:singleLineTitle="false" app:title="@string/hide_single_media_with_card" /> - diff --git a/src/fdroid/fastlane/metadata/android/en/changelogs/469.txt b/src/fdroid/fastlane/metadata/android/en/changelogs/469.txt index 841bc4e6..331a3a69 100644 --- a/src/fdroid/fastlane/metadata/android/en/changelogs/469.txt +++ b/src/fdroid/fastlane/metadata/android/en/changelogs/469.txt @@ -2,6 +2,7 @@ Added: - Maths support (view and compose) - Filter DMs in HOME (long press on the tab) - Filter languages for users in home timeline (from their profile) +- Add several targeted languages for translator Changed: - Hide single media with preview is now a setting (default: disabled)