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;
import android.content.Context;
import org.dslul.openboard.inputmethod.latin.R;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
@ -212,4 +216,17 @@ public final class LocaleUtils {
public static boolean isRtlLanguage(@Nonnull final Locale locale) {
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.recyclerview.widget.RecyclerView
import org.dslul.openboard.inputmethod.latin.R
import org.dslul.openboard.inputmethod.latin.common.LocaleUtils
import org.dslul.openboard.inputmethod.latin.utils.*
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(Settings.getSecondaryLocales(prefs, infos.first().subtype.locale)
.joinToString(", ") {
it.getDisplayName(context.resources.configuration.locale)
LocaleUtils.getLocaleDisplayNameInSystemLocale(it, context)
})
}
text = sb

View file

@ -75,7 +75,7 @@ class LanguageSettingsDialog(
.setItems(displayNames.toTypedArray()) { di, i ->
di.dismiss()
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)
addEnabledSubtype(prefs, newSubtype)
addSubtypeToView(newSubtypeInfo, subtypesView)
@ -145,15 +145,15 @@ class LanguageSettingsDialog(
secondaryLocalesView.findViewById<ImageView>(R.id.add_secondary_language).apply {
isVisible = true
setOnClickListener {
val locales = (availableSecondaryLocales - Settings.getSecondaryLocales(prefs, mainLocaleString).map { it.toString() }).sorted()
val localeNames = locales.map { it.toLocale().getDisplayName(context.resources.configuration.locale) }.toTypedArray()
val locales = (availableSecondaryLocales - Settings.getSecondaryLocales(prefs, mainLocaleString)).sortedBy { it.displayName }
val localeNames = locales.map { LocaleUtils.getLocaleDisplayNameInSystemLocale(it, context) }.toTypedArray()
Builder(context)
.setTitle(R.string.language_selection_title)
.setItems(localeNames) { di, i ->
val locale = locales[i]
val localeStrings = Settings.getSecondaryLocales(prefs, mainLocaleString).map { it.toString() }
Settings.setSecondaryLocales(prefs, mainLocaleString, localeStrings + locale)
addSecondaryLocaleView(locale.toLocale(), secondaryLocalesView)
Settings.setSecondaryLocales(prefs, mainLocaleString, localeStrings + locale.toString())
addSecondaryLocaleView(locale, secondaryLocalesView)
di.dismiss()
}
.show()
@ -231,8 +231,8 @@ class LanguageSettingsDialog(
if (locale != mainLocale) {
val message = context.resources.getString(
R.string.dictionary_file_wrong_locale,
locale.getDisplayName(context.resources.configuration.locale),
mainLocale.getDisplayName(context.resources.configuration.locale)
LocaleUtils.getLocaleDisplayNameInSystemLocale(locale, context),
LocaleUtils.getLocaleDisplayNameInSystemLocale(mainLocale, context)
)
Builder(context)
.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
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 locales = HashSet<String>()
val locales = HashSet<Locale>()
val mainScript = if (asciiCapable) ScriptUtils.SCRIPT_LATIN
else ScriptUtils.getScriptFromSpellCheckerLocale(mainLocale)
// 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
val localeScript = ScriptUtils.getScriptFromSpellCheckerLocale(locale)
if (localeScript != mainScript) continue
locales.add(locale.toString())
locales.add(locale)
}
}
// get assets dictionaries
@ -388,7 +388,7 @@ private fun getAvailableDictionaryLocales(context: Context, mainLocaleString: St
if (locale.language == mainLocale.language) continue
val localeScript = ScriptUtils.getScriptFromSpellCheckerLocale(locale)
if (localeScript != mainScript) continue
locales.add(locale.toString())
locales.add(locale)
}
}
return locales

View file

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