mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-20 21:27:07 +00:00
Added translate all button
This commit is contained in:
parent
53598e3607
commit
99dd73fddd
3 changed files with 115 additions and 24 deletions
|
@ -43,6 +43,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
@ -55,6 +56,7 @@ import androidx.preference.PreferenceManager;
|
|||
import org.jsoup.nodes.Document;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
|
@ -64,6 +66,7 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
|
|||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
final Context context = getContext();
|
||||
final View view = LayoutInflater.from(context).inflate(R.layout.dialog_translate, null);
|
||||
final ImageButton ibAll = view.findViewById(R.id.ibAll);
|
||||
final Spinner spLanguage = view.findViewById(R.id.spLanguage);
|
||||
final TextView tvText = view.findViewById(R.id.tvText);
|
||||
final ContentLoadingProgressBar pbWait = view.findViewById(R.id.pbWait);
|
||||
|
@ -76,6 +79,64 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
|
|||
float textSize = Helper.getTextSize(context, zoom) * message_zoom / 100f;
|
||||
tvText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
||||
|
||||
ibAll.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
DeepL.Language language = (DeepL.Language) spLanguage.getSelectedItem();
|
||||
if (language == null)
|
||||
return;
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", getArguments().getLong("id"));
|
||||
args.putString("target", language.target);
|
||||
|
||||
new SimpleTask<DeepL.Translation>() {
|
||||
@Override
|
||||
protected void onPreExecute(Bundle args) {
|
||||
ibAll.setEnabled(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Bundle args) {
|
||||
ibAll.setEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DeepL.Translation onExecute(Context context, Bundle args) throws Throwable {
|
||||
long id = args.getLong("id");
|
||||
String target = args.getString("target");
|
||||
String text = processMessage(id, context);
|
||||
return DeepL.translate(text, false, target, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, DeepL.Translation translation) {
|
||||
int textColorPrimary = Helper.resolveColor(context, android.R.attr.textColorPrimary);
|
||||
SpannableStringBuilder ssb = new SpannableStringBuilderEx(translation.translated_text);
|
||||
|
||||
ssb.setSpan(new StyleSpan(Typeface.ITALIC), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
ssb.setSpan(new ForegroundColorSpan(textColorPrimary), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
|
||||
Locale source = Locale.forLanguageTag(translation.detected_language);
|
||||
Locale target = Locale.forLanguageTag(args.getString("target"));
|
||||
|
||||
String lang = "[" + source.getDisplayLanguage(target) + "]\n\n";
|
||||
ssb.insert(0, lang);
|
||||
|
||||
ssb.setSpan(new StyleSpan(Typeface.ITALIC), 0, lang.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
ssb.setSpan(new RelativeSizeSpan(HtmlHelper.FONT_SMALL), 0, lang.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
|
||||
tvText.setText(ssb);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
tvText.setText(ex.toString());
|
||||
}
|
||||
}.execute(FragmentDialogTranslate.this, args, "translate:all");
|
||||
}
|
||||
});
|
||||
|
||||
List<DeepL.Language> languages = DeepL.getTargetLanguages(context, false);
|
||||
ArrayAdapter<DeepL.Language> adapter = new ArrayAdapter<DeepL.Language>(context, android.R.layout.simple_spinner_item, android.R.id.text1, languages) {
|
||||
@NonNull
|
||||
|
@ -149,29 +210,7 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
|
|||
@Override
|
||||
protected String onExecute(Context context, Bundle args) throws Throwable {
|
||||
long id = args.getLong("id");
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
EntityMessage message = db.message().getMessage(id);
|
||||
|
||||
File file = EntityMessage.getFile(context, id);
|
||||
String html = Helper.readText(file);
|
||||
Document d = HtmlHelper.sanitizeCompose(context, html, false);
|
||||
|
||||
d.select("blockquote").remove();
|
||||
|
||||
HtmlHelper.truncate(d, HtmlHelper.MAX_TRANSLATABLE_TEXT_SIZE);
|
||||
|
||||
SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null);
|
||||
|
||||
if (message != null && message.subject != null) {
|
||||
ssb.insert(0, "\n\n");
|
||||
ssb.insert(0, message.subject);
|
||||
}
|
||||
|
||||
return ssb.toString()
|
||||
.replace("\uFFFC", "") // Object replacement character
|
||||
.replaceAll("\n\\s+\n", "\n")
|
||||
.replaceAll("\n+", "\n\n");
|
||||
return processMessage(id, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -296,4 +335,29 @@ public class FragmentDialogTranslate extends FragmentDialogBase {
|
|||
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
private static String processMessage(long id, Context context) throws IOException {
|
||||
DB db = DB.getInstance(context);
|
||||
EntityMessage message = db.message().getMessage(id);
|
||||
|
||||
File file = EntityMessage.getFile(context, id);
|
||||
String html = Helper.readText(file);
|
||||
Document d = HtmlHelper.sanitizeCompose(context, html, false);
|
||||
|
||||
d.select("blockquote").remove();
|
||||
|
||||
HtmlHelper.truncate(d, HtmlHelper.MAX_TRANSLATABLE_TEXT_SIZE);
|
||||
|
||||
SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null);
|
||||
|
||||
if (message != null && message.subject != null) {
|
||||
ssb.insert(0, "\n\n");
|
||||
ssb.insert(0, message.subject);
|
||||
}
|
||||
|
||||
return ssb.toString()
|
||||
.replace("\uFFFC", "") // Object replacement character
|
||||
.replaceAll("\n\\s+\n", "\n")
|
||||
.replaceAll("\n+", "\n\n");
|
||||
}
|
||||
}
|
||||
|
|
10
app/src/main/res/drawable/twotone_select_all_24.xml
Normal file
10
app/src/main/res/drawable/twotone_select_all_24.xml
Normal file
|
@ -0,0 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M3,5h2L5,3c-1.1,0 -2,0.9 -2,2zM3,13h2v-2L3,11v2zM7,21h2v-2L7,19v2zM3,9h2L5,7L3,7v2zM13,3h-2v2h2L13,3zM19,3v2h2c0,-1.1 -0.9,-2 -2,-2zM5,21v-2L3,19c0,1.1 0.9,2 2,2zM3,17h2v-2L3,15v2zM9,3L7,3v2h2L9,3zM11,21h2v-2h-2v2zM19,13h2v-2h-2v2zM19,21c1.1,0 2,-0.9 2,-2h-2v2zM19,9h2L21,7h-2v2zM19,17h2v-2h-2v2zM15,21h2v-2h-2v2zM15,5h2L17,3h-2v2zM7,17h10L17,7L7,7v10zM9,9h6v6L9,15L9,9z"/>
|
||||
</vector>
|
|
@ -13,16 +13,33 @@
|
|||
|
||||
<eu.faircode.email.FixedTextView
|
||||
android:id="@+id/tvTitle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:drawableStart="@drawable/twotone_translate_24"
|
||||
android:drawablePadding="6dp"
|
||||
android:gravity="center_vertical"
|
||||
android:minHeight="36dp"
|
||||
android:text="@string/title_translate_tap"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||
android:textColor="?android:attr/textColorPrimary"
|
||||
app:layout_constraintEnd_toEndOf="@+id/ibAll"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<eu.faircode.email.FixedImageButton
|
||||
android:id="@+id/ibAll"
|
||||
android:layout_width="36dp"
|
||||
android:layout_height="36dp"
|
||||
android:background="?android:attr/selectableItemBackgroundBorderless"
|
||||
android:contentDescription="@string/title_info"
|
||||
android:padding="6dp"
|
||||
android:scaleType="fitCenter"
|
||||
android:tooltipText="@string/title_info"
|
||||
app:layout_constraintBottom_toBottomOf="@id/tvTitle"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/tvTitle"
|
||||
app:srcCompat="@drawable/twotone_select_all_24" />
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spLanguage"
|
||||
android:layout_width="0dp"
|
||||
|
|
Loading…
Reference in a new issue