mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-31 06:08:31 +02:00
Experimental support for transliteration
This commit is contained in:
@@ -21,6 +21,8 @@ package eu.faircode.email;
|
||||
|
||||
import android.app.Person;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.icu.text.Transliterator;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.view.textclassifier.ConversationAction;
|
||||
@@ -28,6 +30,9 @@ import android.view.textclassifier.ConversationActions;
|
||||
import android.view.textclassifier.TextClassificationManager;
|
||||
import android.view.textclassifier.TextClassifier;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import java.text.Normalizer;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
@@ -82,6 +87,42 @@ public class TextHelper {
|
||||
}
|
||||
}
|
||||
|
||||
static boolean canTransliterate() {
|
||||
if (!BuildConfig.DEBUG)
|
||||
return false;
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
|
||||
return false;
|
||||
|
||||
try {
|
||||
Transliterator.getInstance("Any-Latin");
|
||||
return true;
|
||||
} catch (Throwable ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static String transliterate(Context context, String text) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
|
||||
return text;
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
boolean notify_transliterate = prefs.getBoolean("notify_transliterate", false);
|
||||
if (!notify_transliterate)
|
||||
return text;
|
||||
|
||||
try {
|
||||
Transliterator t = Transliterator.getInstance("Any-Latin");
|
||||
text = t.transliterate(text);
|
||||
String normalized = Normalizer.normalize(text, Normalizer.Form.NFD);
|
||||
text = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
static ConversationActions getConversationActions(
|
||||
Context context, String[] texts, boolean replies, boolean outgoing, long time) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
|
||||
|
||||
Reference in New Issue
Block a user