- Add several targeted languages for translator

This commit is contained in:
Thomas 2023-01-17 18:08:10 +01:00
parent 3b30708954
commit 255eeac0a5
4 changed files with 70 additions and 21 deletions

View File

@ -20,14 +20,20 @@ import android.os.Build;
import android.text.Html; import android.text.Html;
import android.widget.Toast; import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.github.stom79.mytransl.MyTransL; import com.github.stom79.mytransl.MyTransL;
import com.github.stom79.mytransl.client.HttpsConnectionException; import com.github.stom79.mytransl.client.HttpsConnectionException;
import com.github.stom79.mytransl.client.Results; import com.github.stom79.mytransl.client.Results;
import com.github.stom79.mytransl.translate.Params; 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.R;
import app.fedilab.android.client.entities.app.Languages;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
public class TranslateHelper { public class TranslateHelper {
@ -63,28 +69,72 @@ public class TranslateHelper {
} }
} }
String translate = sharedpreferences.getString(context.getString(R.string.SET_LIVE_TRANSLATE), MyTransL.getLocale()); Set<String> translates = sharedpreferences.getStringSet(context.getString(R.string.SET_LIVE_TRANSLATE_MULTIPLE), null);
if (translate.equalsIgnoreCase("default")) { String translate;
if (translates == null || translates.size() == 0) {
translate = MyTransL.getLocale(); 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
@Override public void onFail(HttpsConnectionException httpsConnectionException) {
public void onSuccess(com.github.stom79.mytransl.translate.Translate translate) {
if (translate.getTranslatedContent() != null) {
callback.onTranslate(translate.getTranslatedContent());
} else {
callback.onTranslate(""); callback.onTranslate("");
Toasty.error(context, context.getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); 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.Language> 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 { public interface Translate {

View File

@ -1267,7 +1267,6 @@
<item>isiZulu</item> <item>isiZulu</item>
</string-array> </string-array>
<string-array name="SET_LIVE_TRANSLATE_ENTRIES" translatable="false"> <string-array name="SET_LIVE_TRANSLATE_ENTRIES" translatable="false">
<item>default</item>
<item>en</item> <item>en</item>
<item>fr</item> <item>fr</item>
<item>de</item> <item>de</item>
@ -1303,7 +1302,6 @@
<item>vi</item> <item>vi</item>
</string-array> </string-array>
<string-array name="SET_LIVE_TRANSLATE_VALUES" translatable="false"> <string-array name="SET_LIVE_TRANSLATE_VALUES" translatable="false">
<item>-----</item>
<item>English</item> <item>English</item>
<item>Français</item> <item>Français</item>
<item>Deutsch</item> <item>Deutsch</item>
@ -1482,7 +1480,7 @@
<string name="SET_GROUP_REBLOGS" translatable="false">SET_GROUP_REBLOGS</string> <string name="SET_GROUP_REBLOGS" translatable="false">SET_GROUP_REBLOGS</string>
<string name="SET_BOOST_ORIGINAL_DATE" translatable="false">SET_BOOST_ORIGINAL_DATE</string> <string name="SET_BOOST_ORIGINAL_DATE" translatable="false">SET_BOOST_ORIGINAL_DATE</string>
<string name="SET_HIDE_SINGLE_MEDIA_WITH_CARD" translatable="false">SET_HIDE_SINGLE_MEDIA_WITH_CARD</string> <string name="SET_HIDE_SINGLE_MEDIA_WITH_CARD" translatable="false">SET_HIDE_SINGLE_MEDIA_WITH_CARD</string>
<string name="SET_LIVE_TRANSLATE" translatable="false">SET_LIVE_TRANSLATE</string> <string name="SET_LIVE_TRANSLATE_MULTIPLE" translatable="false">SET_LIVE_TRANSLATE_MULTIPLE</string>
<string name="SET_TRUNCATE_TOOTS_SIZE" translatable="false">SET_TRUNCATE_TOOTS_SIZE</string> <string name="SET_TRUNCATE_TOOTS_SIZE" translatable="false">SET_TRUNCATE_TOOTS_SIZE</string>
<string name="SET_ART_WITH_NSFW" translatable="false">SET_ART_WITH_NSFW</string> <string name="SET_ART_WITH_NSFW" translatable="false">SET_ART_WITH_NSFW</string>
<string name="SET_VIDEO_CACHE" translatable="false">SET_VIDEO_CACHE</string> <string name="SET_VIDEO_CACHE" translatable="false">SET_VIDEO_CACHE</string>
@ -2229,4 +2227,5 @@
<string name="hide_single_media_with_card">Hide single media when there is a link preview</string> <string name="hide_single_media_with_card">Hide single media when there is a link preview</string>
<string name="action_change_subscribed_language">Change subscribed languages</string> <string name="action_change_subscribed_language">Change subscribed languages</string>
<string name="filter_languages">Filter languages</string> <string name="filter_languages">Filter languages</string>
<string name="translate_in">Translate in</string>
</resources> </resources>

View File

@ -38,12 +38,11 @@
app:key="@string/SET_HIDE_SINGLE_MEDIA_WITH_CARD" app:key="@string/SET_HIDE_SINGLE_MEDIA_WITH_CARD"
app:singleLineTitle="false" app:singleLineTitle="false"
app:title="@string/hide_single_media_with_card" /> app:title="@string/hide_single_media_with_card" />
<ListPreference <MultiSelectListPreference
android:defaultValue="default"
app:entries="@array/SET_LIVE_TRANSLATE_VALUES" app:entries="@array/SET_LIVE_TRANSLATE_VALUES"
app:entryValues="@array/SET_LIVE_TRANSLATE_ENTRIES" app:entryValues="@array/SET_LIVE_TRANSLATE_ENTRIES"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:key="@string/SET_LIVE_TRANSLATE" app:key="@string/SET_LIVE_TRANSLATE_MULTIPLE"
app:summary="@string/set_live_translate" app:summary="@string/set_live_translate"
app:title="@string/set_live_translate_title" app:title="@string/set_live_translate_title"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />

View File

@ -2,6 +2,7 @@ Added:
- Maths support (view and compose) - Maths support (view and compose)
- Filter DMs in HOME (long press on the tab) - Filter DMs in HOME (long press on the tab)
- Filter languages for users in home timeline (from their profile) - Filter languages for users in home timeline (from their profile)
- Add several targeted languages for translator
Changed: Changed:
- Hide single media with preview is now a setting (default: disabled) - Hide single media with preview is now a setting (default: disabled)