DeepL: added formal form option

This commit is contained in:
M66B 2022-02-03 12:47:52 +01:00
parent a87c4bb643
commit 0f438a829f
6 changed files with 64 additions and 3 deletions

View File

@ -5,6 +5,7 @@
### Next version ### Next version
* Added slider to change message column width * Added slider to change message column width
* Added option for formal/informal DeepL translation
* Small improvements and minor bug fixes * Small improvements and minor bug fixes
### [Epidexipteryx](https://en.wikipedia.org/wiki/Epidexipteryx) ### [Epidexipteryx](https://en.wikipedia.org/wiki/Epidexipteryx)

View File

@ -5,6 +5,7 @@
### Next version ### Next version
* Added slider to change message column width * Added slider to change message column width
* Added option for formal/informal DeepL translation
* Small improvements and minor bug fixes * Small improvements and minor bug fixes
### [Epidexipteryx](https://en.wikipedia.org/wiki/Epidexipteryx) ### [Epidexipteryx](https://en.wikipedia.org/wiki/Epidexipteryx)

View File

@ -60,6 +60,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -100,6 +101,7 @@ public class DeepL {
JSONObject jlanguage = jlanguages.getJSONObject(i); JSONObject jlanguage = jlanguages.getJSONObject(i);
String name = jlanguage.getString("name"); String name = jlanguage.getString("name");
String target = jlanguage.getString("language"); String target = jlanguage.getString("language");
boolean formality = jlanguage.optBoolean("supports_formality");
Locale locale = Locale.forLanguageTag(target); Locale locale = Locale.forLanguageTag(target);
if (locale != null) if (locale != null)
@ -110,7 +112,7 @@ public class DeepL {
String resname = "language_" + target.toLowerCase().replace('-', '_'); String resname = "language_" + target.toLowerCase().replace('-', '_');
int resid = res.getIdentifier(resname, "drawable", pkg); int resid = res.getIdentifier(resname, "drawable", pkg);
languages.add(new Language(name, target, languages.add(new Language(name, target, formality,
resid == 0 ? null : resid, resid == 0 ? null : resid,
favorites && frequency > 0)); favorites && frequency > 0));
frequencies.put(target, frequency); frequencies.put(target, frequency);
@ -188,11 +190,28 @@ public class DeepL {
} }
public static Translation translate(String text, String target, Context context) throws IOException, JSONException { public static Translation translate(String text, String target, Context context) throws IOException, JSONException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean formality = prefs.getBoolean("deepl_formal", true);
return translate(text, target, formality, context);
}
public static Translation translate(String text, String target, boolean formality, Context context) throws IOException, JSONException {
// https://www.deepl.com/docs-api/translating-text/request/ // https://www.deepl.com/docs-api/translating-text/request/
String request = String request =
"text=" + URLEncoder.encode(text, StandardCharsets.UTF_8.name()) + "text=" + URLEncoder.encode(text, StandardCharsets.UTF_8.name()) +
"&target_lang=" + URLEncoder.encode(target, StandardCharsets.UTF_8.name()); "&target_lang=" + URLEncoder.encode(target, StandardCharsets.UTF_8.name());
ensureLanguages(context);
for (int i = 0; i < jlanguages.length(); i++) {
JSONObject jlanguage = jlanguages.getJSONObject(i);
if (Objects.equals(target, jlanguage.getString("language"))) {
boolean supports_formality = jlanguage.optBoolean("supports_formality");
if (supports_formality)
request += "&formality=" + (formality ? "more" : "less");
break;
}
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = prefs.getString("deepl_key", null); String key = prefs.getString("deepl_key", null);
@ -289,12 +308,14 @@ public class DeepL {
public static class Language { public static class Language {
public String name; public String name;
public String target; public String target;
public boolean formality;
public Integer icon; public Integer icon;
public boolean favorite; public boolean favorite;
private Language(String name, String target, Integer icon, boolean favorit) { private Language(String name, String target, boolean formality, Integer icon, boolean favorit) {
this.name = name; this.name = name;
this.target = target; this.target = target;
this.formality = formality;
this.icon = icon; this.icon = icon;
this.favorite = favorit; this.favorite = favorit;
} }
@ -319,12 +340,15 @@ public class DeepL {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = prefs.getString("deepl_key", null); String key = prefs.getString("deepl_key", null);
boolean pro = prefs.getBoolean("deepl_pro", false); boolean pro = prefs.getBoolean("deepl_pro", false);
boolean formal = prefs.getBoolean("deepl_formal", true);
boolean small = prefs.getBoolean("deepl_small", false); boolean small = prefs.getBoolean("deepl_small", false);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_deepl, null); View view = LayoutInflater.from(context).inflate(R.layout.dialog_deepl, null);
final ImageButton ibInfo = view.findViewById(R.id.ibInfo); final ImageButton ibInfo = view.findViewById(R.id.ibInfo);
final EditText etKey = view.findViewById(R.id.etKey); final EditText etKey = view.findViewById(R.id.etKey);
final CheckBox cbPro = view.findViewById(R.id.cbPro); final CheckBox cbPro = view.findViewById(R.id.cbPro);
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 cbSmall = view.findViewById(R.id.cbSmall);
final TextView tvUsage = view.findViewById(R.id.tvUsage); final TextView tvUsage = view.findViewById(R.id.tvUsage);
final TextView tvPrivacy = view.findViewById(R.id.tvPrivacy); final TextView tvPrivacy = view.findViewById(R.id.tvPrivacy);
@ -353,6 +377,19 @@ public class DeepL {
etKey.setText(key); etKey.setText(key);
cbPro.setChecked(pro); cbPro.setChecked(pro);
cbFormal.setChecked(formal);
try {
List<String> formals = new ArrayList<>();
for (Language lang : getTargetLanguages(context, false))
if (lang.formality)
formals.add(lang.name);
tvFormal.setText(TextUtils.join(", ", formals));
} catch (Throwable ex) {
tvFormal.setText(Log.formatThrowable(ex, false));
}
cbSmall.setChecked(small); cbSmall.setChecked(small);
tvUsage.setVisibility(View.GONE); tvUsage.setVisibility(View.GONE);
@ -396,6 +433,7 @@ public class DeepL {
else else
editor.putString("deepl_key", key); editor.putString("deepl_key", key);
editor.putBoolean("deepl_pro", cbPro.isChecked()); editor.putBoolean("deepl_pro", cbPro.isChecked());
editor.putBoolean("deepl_formal", cbFormal.isChecked());
editor.putBoolean("deepl_small", cbSmall.isChecked()); editor.putBoolean("deepl_small", cbSmall.isChecked());
editor.apply(); editor.apply();
} }

View File

@ -62,6 +62,25 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etKey" /> app:layout_constraintTop_toBottomOf="@id/etKey" />
<CheckBox
android:id="@+id/cbFormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_translate_formal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbPro" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvFormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="Languages"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbFormal" />
<CheckBox <CheckBox
android:id="@+id/cbSmall" android:id="@+id/cbSmall"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -69,7 +88,7 @@
android:layout_marginTop="12dp" android:layout_marginTop="12dp"
android:text="@string/title_translate_small" android:text="@string/title_translate_small"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbPro" /> app:layout_constraintTop_toBottomOf="@id/tvFormal" />
<eu.faircode.email.FixedTextView <eu.faircode.email.FixedTextView
android:id="@+id/tvUsage" android:id="@+id/tvUsage"

View File

@ -1316,6 +1316,7 @@
<string name="title_translate_configure">Configure &#8230;</string> <string name="title_translate_configure">Configure &#8230;</string>
<string name="title_translate_key">Enter key</string> <string name="title_translate_key">Enter key</string>
<string name="title_translating">Translating &#8230;</string> <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_small">Use a small font for the source text</string>
<string name="title_translate_usage">Usage: %1$s / %2$s (%3$d %%)</string> <string name="title_translate_usage">Usage: %1$s / %2$s (%3$d %%)</string>
<string name="title_translate_tap">Tap the text to be translated</string> <string name="title_translate_tap">Tap the text to be translated</string>

View File

@ -5,6 +5,7 @@
### Next version ### Next version
* Added slider to change message column width * Added slider to change message column width
* Added option for formal/informal DeepL translation
* Small improvements and minor bug fixes * Small improvements and minor bug fixes
### [Epidexipteryx](https://en.wikipedia.org/wiki/Epidexipteryx) ### [Epidexipteryx](https://en.wikipedia.org/wiki/Epidexipteryx)