Select DeepL target language

pull/199/head
M66B 4 years ago
parent daf4aa4511
commit 4eee5f9e72

@ -96,6 +96,8 @@ import android.view.textclassifier.ConversationAction;
import android.view.textclassifier.ConversationActions;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
@ -103,6 +105,7 @@ import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@ -6661,9 +6664,36 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext();
final View view = LayoutInflater.from(context).inflate(R.layout.dialog_translate, null);
final Spinner spLanguage = view.findViewById(R.id.spLanguage);
final TextView tvText = view.findViewById(R.id.tvText);
final ContentLoadingProgressBar pbWait = view.findViewById(R.id.pbWait);
List<DeepL.Language> languages = DeepL.getTargetLanguages(context);
ArrayAdapter<DeepL.Language> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, android.R.id.text1, languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLanguage.setAdapter(adapter);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String current = prefs.getString("deepl_target", null);
for (int pos = 0; pos < languages.size(); pos++)
if (languages.get(pos).target.equals(current)) {
spLanguage.setSelection(pos);
break;
}
spLanguage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
prefs.edit().putString("deepl_target", languages.get(position).target).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("deepl_target").apply();
}
});
tvText.setText(null);
new SimpleTask<String>() {
@ -6726,7 +6756,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
final StyleSpan mark = new StyleSpan(Typeface.ITALIC);
buffer.setSpan(mark, start, end, 0);
DeepL.Language language = (DeepL.Language) spLanguage.getSelectedItem();
if (language == null)
return;
Bundle args = new Bundle();
args.putString("target", language.target);
args.putString("text", buffer.subSequence(start, end).toString());
new SimpleTask<DeepL.Translation>() {
@ -6743,8 +6778,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override
protected DeepL.Translation onExecute(Context context, Bundle args) throws Throwable {
String text = args.getString("text");
String language = DeepL.getCurrentLanguage(context);
return DeepL.translate(text, language, context);
String target = args.getString("target");
return DeepL.translate(text, target, context);
}
@Override
@ -7110,9 +7145,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
final CheckBox cbUnsubscribe = dview.findViewById(R.id.cbUnsubscribe);
final CheckBox cbRule = dview.findViewById(R.id.cbRule);
cbTranslate.setVisibility(DeepL.isAvailable(context) &&
DeepL.getCurrentLanguage(context) != null
? View.VISIBLE : View.GONE);
cbTranslate.setVisibility(DeepL.isAvailable(context) ? View.VISIBLE : View.GONE);
cbPin.setVisibility(Shortcuts.can(context) ? View.VISIBLE : View.GONE);
cbJunk.setChecked(prefs.getBoolean("button_junk", true));

@ -135,40 +135,6 @@ public class DeepL {
}
}
public static String getCurrentLanguage(Context context) {
try {
ensureLanguages(context);
Locale locale = Locale.getDefault();
String tag = locale.toLanguageTag(); // en-US
String lang = locale.getLanguage(); // en
if ("en".equalsIgnoreCase(lang))
if ("EN-GB".equalsIgnoreCase(tag) || "EN-US".equalsIgnoreCase(tag))
lang = tag;
else
lang = "EN-US";
if ("pt".equalsIgnoreCase(lang))
if ("PT-BR".equalsIgnoreCase(tag) || "PT-PT".equalsIgnoreCase(tag))
lang = tag;
else
lang = "PT-PT";
for (int i = 0; i < jlanguages.length(); i++) {
JSONObject jlanguage = jlanguages.getJSONObject(i);
String language = jlanguage.getString("language");
if (language.equalsIgnoreCase(lang)) {
return language;
}
}
} catch (Throwable ex) {
Log.e(ex);
}
return null;
}
private static void ensureLanguages(Context context) throws IOException, JSONException {
if (jlanguages != null)
return;
@ -321,6 +287,11 @@ public class DeepL {
this.target = target;
this.icon = icon;
}
@Override
public String toString() {
return name;
}
}
public static class Translation {

@ -16,11 +16,33 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="@+id/etKeyword"
android:text="@string/title_translate_tap"
android:text="@string/title_translate"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spLanguage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvUsage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_translate_tap"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="?android:textColorPrimary"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spLanguage" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvText"
android:layout_width="0dp"
@ -31,7 +53,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />
app:layout_constraintTop_toBottomOf="@id/tvUsage" />
<eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbWait"

Loading…
Cancel
Save