avoid displaying <Language> (Unknown Region)

This commit is contained in:
Helium314 2023-08-28 08:15:10 +02:00
parent a81726f8dc
commit 0687d10420
4 changed files with 34 additions and 23 deletions

View file

@ -16,6 +16,10 @@
package org.dslul.openboard.inputmethod.latin.common; package org.dslul.openboard.inputmethod.latin.common;
import android.content.Context;
import org.dslul.openboard.inputmethod.latin.R;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
@ -212,4 +216,17 @@ public final class LocaleUtils {
public static boolean isRtlLanguage(@Nonnull final Locale locale) { public static boolean isRtlLanguage(@Nonnull final Locale locale) {
return sRtlLanguageCodes.contains(locale.getLanguage()); return sRtlLanguageCodes.contains(locale.getLanguage());
} }
public static String getLocaleDisplayNameInSystemLocale(final Locale locale, final Context context) {
final String localeString = locale.toString();
if (localeString.equals("zz"))
return context.getString(R.string.subtype_no_language);
if (localeString.endsWith("_ZZ") || localeString.endsWith("_zz")) {
final int resId = context.getResources()
.getIdentifier("subtype_"+localeString, "string", context.getPackageName());
if (resId != 0)
return context.getString(resId);
}
return locale.getDisplayName(context.getResources().getConfiguration().locale);
}
} }

View file

@ -19,6 +19,7 @@ import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import org.dslul.openboard.inputmethod.latin.R import org.dslul.openboard.inputmethod.latin.R
import org.dslul.openboard.inputmethod.latin.common.LocaleUtils
import org.dslul.openboard.inputmethod.latin.utils.* import org.dslul.openboard.inputmethod.latin.utils.*
class LanguageFilterListPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) { class LanguageFilterListPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
@ -116,7 +117,7 @@ class LanguageAdapter(list: List<MutableList<SubtypeInfo>> = listOf(), context:
sb.append("\n") sb.append("\n")
sb.append(Settings.getSecondaryLocales(prefs, infos.first().subtype.locale) sb.append(Settings.getSecondaryLocales(prefs, infos.first().subtype.locale)
.joinToString(", ") { .joinToString(", ") {
it.getDisplayName(context.resources.configuration.locale) LocaleUtils.getLocaleDisplayNameInSystemLocale(it, context)
}) })
} }
text = sb text = sb

View file

@ -75,7 +75,7 @@ class LanguageSettingsDialog(
.setItems(displayNames.toTypedArray()) { di, i -> .setItems(displayNames.toTypedArray()) { di, i ->
di.dismiss() di.dismiss()
val newSubtype = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(mainLocaleString, layouts[i]) val newSubtype = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(mainLocaleString, layouts[i])
val newSubtypeInfo = newSubtype.toSubtypeInfo(mainLocale, context.resources, true) // enabled by default, because why else add them val newSubtypeInfo = newSubtype.toSubtypeInfo(mainLocale, context, true) // enabled by default, because why else add them
addAdditionalSubtype(prefs, context.resources, newSubtype) addAdditionalSubtype(prefs, context.resources, newSubtype)
addEnabledSubtype(prefs, newSubtype) addEnabledSubtype(prefs, newSubtype)
addSubtypeToView(newSubtypeInfo, subtypesView) addSubtypeToView(newSubtypeInfo, subtypesView)
@ -145,15 +145,15 @@ class LanguageSettingsDialog(
secondaryLocalesView.findViewById<ImageView>(R.id.add_secondary_language).apply { secondaryLocalesView.findViewById<ImageView>(R.id.add_secondary_language).apply {
isVisible = true isVisible = true
setOnClickListener { setOnClickListener {
val locales = (availableSecondaryLocales - Settings.getSecondaryLocales(prefs, mainLocaleString).map { it.toString() }).sorted() val locales = (availableSecondaryLocales - Settings.getSecondaryLocales(prefs, mainLocaleString)).sortedBy { it.displayName }
val localeNames = locales.map { it.toLocale().getDisplayName(context.resources.configuration.locale) }.toTypedArray() val localeNames = locales.map { LocaleUtils.getLocaleDisplayNameInSystemLocale(it, context) }.toTypedArray()
Builder(context) Builder(context)
.setTitle(R.string.language_selection_title) .setTitle(R.string.language_selection_title)
.setItems(localeNames) { di, i -> .setItems(localeNames) { di, i ->
val locale = locales[i] val locale = locales[i]
val localeStrings = Settings.getSecondaryLocales(prefs, mainLocaleString).map { it.toString() } val localeStrings = Settings.getSecondaryLocales(prefs, mainLocaleString).map { it.toString() }
Settings.setSecondaryLocales(prefs, mainLocaleString, localeStrings + locale) Settings.setSecondaryLocales(prefs, mainLocaleString, localeStrings + locale.toString())
addSecondaryLocaleView(locale.toLocale(), secondaryLocalesView) addSecondaryLocaleView(locale, secondaryLocalesView)
di.dismiss() di.dismiss()
} }
.show() .show()
@ -231,8 +231,8 @@ class LanguageSettingsDialog(
if (locale != mainLocale) { if (locale != mainLocale) {
val message = context.resources.getString( val message = context.resources.getString(
R.string.dictionary_file_wrong_locale, R.string.dictionary_file_wrong_locale,
locale.getDisplayName(context.resources.configuration.locale), LocaleUtils.getLocaleDisplayNameInSystemLocale(locale, context),
mainLocale.getDisplayName(context.resources.configuration.locale) LocaleUtils.getLocaleDisplayNameInSystemLocale(mainLocale, context)
) )
Builder(context) Builder(context)
.setMessage(message) .setMessage(message)
@ -351,9 +351,9 @@ fun getUserAndInternalDictionaries(context: Context, locale: String): Pair<List<
} }
// get locales with same script as main locale, but different language // get locales with same script as main locale, but different language
private fun getAvailableDictionaryLocales(context: Context, mainLocaleString: String, asciiCapable: Boolean): Set<String> { private fun getAvailableDictionaryLocales(context: Context, mainLocaleString: String, asciiCapable: Boolean): Set<Locale> {
val mainLocale = mainLocaleString.toLocale() val mainLocale = mainLocaleString.toLocale()
val locales = HashSet<String>() val locales = HashSet<Locale>()
val mainScript = if (asciiCapable) ScriptUtils.SCRIPT_LATIN val mainScript = if (asciiCapable) ScriptUtils.SCRIPT_LATIN
else ScriptUtils.getScriptFromSpellCheckerLocale(mainLocale) else ScriptUtils.getScriptFromSpellCheckerLocale(mainLocale)
// ScriptUtils.getScriptFromSpellCheckerLocale may return latin when it should not // ScriptUtils.getScriptFromSpellCheckerLocale may return latin when it should not
@ -373,7 +373,7 @@ private fun getAvailableDictionaryLocales(context: Context, mainLocaleString: St
if (locale.language == mainLocale.language) continue if (locale.language == mainLocale.language) continue
val localeScript = ScriptUtils.getScriptFromSpellCheckerLocale(locale) val localeScript = ScriptUtils.getScriptFromSpellCheckerLocale(locale)
if (localeScript != mainScript) continue if (localeScript != mainScript) continue
locales.add(locale.toString()) locales.add(locale)
} }
} }
// get assets dictionaries // get assets dictionaries
@ -388,7 +388,7 @@ private fun getAvailableDictionaryLocales(context: Context, mainLocaleString: St
if (locale.language == mainLocale.language) continue if (locale.language == mainLocale.language) continue
val localeScript = ScriptUtils.getScriptFromSpellCheckerLocale(locale) val localeScript = ScriptUtils.getScriptFromSpellCheckerLocale(locale)
if (localeScript != mainScript) continue if (localeScript != mainScript) continue
locales.add(locale.toString()) locales.add(locale)
} }
} }
return locales return locales

View file

@ -1,9 +1,9 @@
package org.dslul.openboard.inputmethod.latin.settings package org.dslul.openboard.inputmethod.latin.settings
import android.app.Activity import android.app.Activity
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.Resources
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.preference.TwoStatePreference import android.preference.TwoStatePreference
@ -137,7 +137,7 @@ class LanguageSettingsFragment : SubScreenFragment() {
} }
private fun InputMethodSubtype.toSubtypeInfo(locale: Locale, isEnabled: Boolean = false) = private fun InputMethodSubtype.toSubtypeInfo(locale: Locale, isEnabled: Boolean = false) =
toSubtypeInfo(locale, resources, isEnabled) toSubtypeInfo(locale, activity, isEnabled)
private fun List<SubtypeInfo>.addToSortedSubtypes() { private fun List<SubtypeInfo>.addToSortedSubtypes() {
forEach { forEach {
@ -180,15 +180,8 @@ class SubtypeInfo(val displayName: String, val subtype: InputMethodSubtype, var
} }
} }
fun InputMethodSubtype.toSubtypeInfo(locale: Locale, resources: Resources, isEnabled: Boolean): SubtypeInfo { fun InputMethodSubtype.toSubtypeInfo(locale: Locale, context: Context, isEnabled: Boolean): SubtypeInfo =
val displayName = if (locale.toString().equals("zz", true)) // no language SubtypeInfo(LocaleUtils.getLocaleDisplayNameInSystemLocale(locale, context), this, isEnabled)
SubtypeLocaleUtils.getSubtypeLocaleDisplayNameInSystemLocale(locale.toString())
else if (locale.toString().endsWith("zz", true)) // serbian (latin), maybe others in the future
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(this)
else
locale.getDisplayName(resources.configuration.locale)
return SubtypeInfo(displayName, this, isEnabled)
}
private const val DICTIONARY_REQUEST_CODE = 96834 private const val DICTIONARY_REQUEST_CODE = 96834
const val USER_DICTIONARY_SUFFIX = "user.dict" const val USER_DICTIONARY_SUFFIX = "user.dict"