diff --git a/app/build.gradle b/app/build.gradle index ee7b9ea17d..abcd0af384 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,7 +31,7 @@ android { //ndkVersion "21.0.6113669" ndk { - // Bugsnag + // Bugsnag, sqlite abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64" } } @@ -250,6 +250,7 @@ dependencies { def exif_version = "1.3.0-alpha01" def biometric_version = "1.0.1" // https://issuetracker.google.com/issues/159983244 def textclassifier_version = "1.0.0-alpha03" + def mlkit_identify = "16.0.0" def mlkit_translate = "16.0.0" def billingclient_version = "3.0.0" def javamail_version = "1.6.5" @@ -344,7 +345,9 @@ dependencies { // https://developer.android.com/jetpack/androidx/releases/textclassifier //implementation "androidx.textclassifier:textclassifier:$textclassifier_version" + // https://developers.google.com/ml-kit/language/identification/android // https://developers.google.com/ml-kit/language/translation/android + debugImplementation "com.google.mlkit:language-id:$mlkit_identify" debugImplementation "com.google.mlkit:translate:$mlkit_translate" // https://developer.android.com/google/play/billing/billing_library_releases_notes diff --git a/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java b/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java index 2afe0f4262..2ec28fed98 100644 --- a/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java +++ b/app/src/xlat/java/eu/faircode/email/FragmentDialogTranslate.java @@ -11,6 +11,9 @@ import androidx.annotation.Nullable; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.mlkit.common.model.DownloadConditions; +import com.google.mlkit.nl.languageid.LanguageIdentification; +import com.google.mlkit.nl.languageid.LanguageIdentificationOptions; +import com.google.mlkit.nl.languageid.LanguageIdentifier; import com.google.mlkit.nl.translate.TranslateLanguage; import com.google.mlkit.nl.translate.Translation; import com.google.mlkit.nl.translate.Translator; @@ -39,28 +42,50 @@ public class FragmentDialogTranslate extends FragmentDialogBase { .setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - String language = map.get(items[which]); + String targetLanguage = map.get(items[which]); - TranslatorOptions options = new TranslatorOptions.Builder() - .setSourceLanguage(TranslateLanguage.ENGLISH) - .setTargetLanguage(language) - .build(); - Translator translator = Translation.getClient(options); - DownloadConditions conditions = new DownloadConditions.Builder() - .requireWifi() - .build(); - translator.downloadModelIfNeeded(conditions) + LanguageIdentifier languageIdentifier = LanguageIdentification.getClient( + new LanguageIdentificationOptions.Builder() + .setConfidenceThreshold(0.34f) + .build()); + languageIdentifier.identifyLanguage(text) .addOnSuccessListener( - new OnSuccessListener() { + new OnSuccessListener() { @Override - public void onSuccess(Void v) { - translator.translate(text) + public void onSuccess(@Nullable String sourceLanguage) { + Log.i("Translate source=" + sourceLanguage); + if (sourceLanguage.equals("und")) + sourceLanguage = TranslateLanguage.ENGLISH; + + TranslatorOptions options = new TranslatorOptions.Builder() + .setSourceLanguage(sourceLanguage) + .setTargetLanguage(targetLanguage) + .build(); + Translator translator = Translation.getClient(options); + DownloadConditions conditions = new DownloadConditions.Builder() + .requireWifi() + .build(); + translator.downloadModelIfNeeded(conditions) .addOnSuccessListener( - new OnSuccessListener() { + new OnSuccessListener() { @Override - public void onSuccess(@NonNull String translatedText) { - getArguments().putString("translated", translatedText); - sendResult(RESULT_OK); + public void onSuccess(Void v) { + translator.translate(text) + .addOnSuccessListener( + new OnSuccessListener() { + @Override + public void onSuccess(@NonNull String translatedText) { + getArguments().putString("translated", translatedText); + sendResult(RESULT_OK); + } + }) + .addOnFailureListener( + new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }); } }) .addOnFailureListener(