Added option to replace source text by translated text

pull/212/head
M66B 1 year ago
parent 11ecd31633
commit 839d1842b1

@ -33,6 +33,7 @@ import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;
@ -349,6 +350,7 @@ public class DeepL {
String key = prefs.getString("deepl_key", null);
boolean formal = prefs.getBoolean("deepl_formal", true);
boolean small = prefs.getBoolean("deepl_small", false);
boolean replace = prefs.getBoolean("deepl_replace", false);
boolean html = prefs.getBoolean("deepl_html", false);
int subscription = prefs.getInt("deepl_subscription", BuildConfig.DEBUG ? 17 : 0);
@ -358,6 +360,7 @@ public class DeepL {
final CheckBox cbFormal = view.findViewById(R.id.cbFormal);
final TextView tvFormal = view.findViewById(R.id.tvFormal);
final CheckBox cbSmall = view.findViewById(R.id.cbSmall);
final CheckBox cbReplace = view.findViewById(R.id.cbReplace);
final CheckBox cbHtml = view.findViewById(R.id.cbHtml);
final TextView tvUsage = view.findViewById(R.id.tvUsage);
final TextView tvPrivacy = view.findViewById(R.id.tvPrivacy);
@ -369,6 +372,13 @@ public class DeepL {
}
});
cbSmall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
cbReplace.setEnabled(!isChecked);
}
});
tvUsage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -399,6 +409,8 @@ public class DeepL {
}
cbSmall.setChecked(small);
cbReplace.setChecked(replace);
cbReplace.setEnabled(!small);
cbHtml.setChecked(html);
tvUsage.setVisibility(View.GONE);
@ -461,6 +473,7 @@ public class DeepL {
editor.putString("deepl_key", key);
editor.putBoolean("deepl_formal", cbFormal.isChecked());
editor.putBoolean("deepl_small", cbSmall.isChecked());
editor.putBoolean("deepl_replace", cbReplace.isChecked());
editor.putBoolean("deepl_html", cbHtml.isChecked());
editor.apply();
}

@ -2751,6 +2751,8 @@ public class FragmentCompose extends FragmentBase {
return;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean small = prefs.getBoolean("deepl_small", false);
boolean replace = prefs.getBoolean("deepl_replace", false);
// Insert translated text
/*
@ -2773,22 +2775,28 @@ public class FragmentCompose extends FragmentBase {
at android.text.SpannableStringBuilder.insert(SpannableStringBuilder.java:226)
at android.text.SpannableStringBuilder.insert(SpannableStringBuilder.java:38)
*/
int len = 2 + translation.translated_text.length();
int len = translation.translated_text.length();
edit.insert(paragraph.second, translation.translated_text);
edit.insert(paragraph.second, "\n\n");
StyleHelper.markAsInserted(edit, paragraph.second, paragraph.second + len);
if (!replace) {
edit.insert(paragraph.second, "\n\n");
len += 2;
}
StyleHelper.markAsInserted(edit, paragraph.second, paragraph.second + len);
etBody.setSelection(paragraph.second + len);
boolean small = prefs.getBoolean("deepl_small", false);
if (small) {
RelativeSizeSpan[] spans = edit.getSpans(
paragraph.first, paragraph.second, RelativeSizeSpan.class);
for (RelativeSizeSpan span : spans)
edit.removeSpan(span);
edit.setSpan(new RelativeSizeSpan(BuildConfig.DEBUG ? HtmlHelper.FONT_XSMALL : HtmlHelper.FONT_SMALL),
edit.setSpan(new RelativeSizeSpan(HtmlHelper.FONT_SMALL),
paragraph.first, paragraph.second,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (replace) {
edit.delete(paragraph.first, paragraph.second);
}
// Updated frequency

@ -89,6 +89,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvFormal" />
<CheckBox
android:id="@+id/cbReplace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="@string/title_translate_replace"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbSmall" />
<CheckBox
android:id="@+id/cbHtml"
android:layout_width="wrap_content"
@ -96,7 +106,7 @@
android:layout_marginTop="12dp"
android:text="@string/title_translate_html"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbSmall" />
app:layout_constraintTop_toBottomOf="@id/cbReplace" />
<TextView
android:id="@+id/tvHtml"

@ -1631,6 +1631,7 @@
<string name="title_translating">Translating &#8230;</string>
<string name="title_translate_formal">Use formal form</string>
<string name="title_translate_small">Use a small font for the source text</string>
<string name="title_translate_replace">Replace source text by translated text</string>
<string name="title_translate_html">Preserve formatting</string>
<string name="title_translate_html_hint">This will consume more characters</string>
<string name="title_translate_usage">Usage: %1$s / %2$s (%3$d %%)</string>

Loading…
Cancel
Save