workaround for languages that don't have a display name in all languages

This commit is contained in:
Helium314 2024-04-29 23:41:36 +02:00
parent dc4cdea687
commit 4476456b83
2 changed files with 21 additions and 5 deletions

View file

@ -6,7 +6,9 @@
package helium314.keyboard.latin.common package helium314.keyboard.latin.common
import android.content.Context import android.content.Context
import android.content.res.Resources
import helium314.keyboard.compat.locale import helium314.keyboard.compat.locale
import helium314.keyboard.latin.BuildConfig
import helium314.keyboard.latin.R import helium314.keyboard.latin.R
import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SubtypeLocaleUtils import helium314.keyboard.latin.utils.SubtypeLocaleUtils
@ -181,12 +183,26 @@ object LocaleUtils {
@JvmStatic @JvmStatic
fun getLocaleDisplayNameInSystemLocale(locale: Locale, context: Context): String { fun getLocaleDisplayNameInSystemLocale(locale: Locale, context: Context): String {
return getLocaleDisplayNameInLocale(locale, context.resources, context.resources.configuration.locale())
}
@JvmStatic
fun getLocaleDisplayNameInLocale(locale: Locale, resources: Resources, displayLocale: Locale): String {
val languageTag = locale.toLanguageTag() val languageTag = locale.toLanguageTag()
if (languageTag == SubtypeLocaleUtils.NO_LANGUAGE) return context.getString(R.string.subtype_no_language) if (languageTag == SubtypeLocaleUtils.NO_LANGUAGE) return resources.getString(R.string.subtype_no_language)
if (locale.script() != locale.language.constructLocale().script() || locale.language == "xdq") { if (locale.script() != locale.language.constructLocale().script() || locale.language == "xdq") {
val resId = context.resources.getIdentifier("subtype_${languageTag.replace("-", "_")}", "string", context.packageName) val resId = resources.getIdentifier(
if (resId != 0) return context.getString(resId) "subtype_${languageTag.replace("-", "_")}",
"string",
BuildConfig.APPLICATION_ID // replaces context.packageName, see https://stackoverflow.com/a/24525379
)
if (resId != 0) return resources.getString(resId)
}
val localeDisplayName = locale.getDisplayName(displayLocale)
return if (localeDisplayName == languageTag) {
locale.getDisplayName(Locale.US) // try fallback to English name, relevant e.g. fpr pms, see https://github.com/Helium314/HeliBoard/pull/748
} else {
localeDisplayName
} }
return locale.getDisplayName(context.resources.configuration.locale())
} }
} }

View file

@ -192,7 +192,7 @@ public final class SubtypeLocaleUtils {
if (exceptionalNameResId != null) { if (exceptionalNameResId != null) {
displayName = RunInLocaleKt.runInLocale(sResources, displayLocale, res -> res.getString(exceptionalNameResId)); displayName = RunInLocaleKt.runInLocale(sResources, displayLocale, res -> res.getString(exceptionalNameResId));
} else { } else {
displayName = locale.getDisplayName(displayLocale); displayName = LocaleUtils.getLocaleDisplayNameInLocale(locale, sResources, displayLocale);
} }
return StringUtils.capitalizeFirstCodePoint(displayName, displayLocale); return StringUtils.capitalizeFirstCodePoint(displayName, displayLocale);
} }