Skip to content

Commit

Permalink
workaround for languages that don't have a display name in all languages
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 committed Apr 29, 2024
1 parent dc4cdea commit 4476456
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions app/src/main/java/helium314/keyboard/latin/common/LocaleUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package helium314.keyboard.latin.common

import android.content.Context
import android.content.res.Resources
import helium314.keyboard.compat.locale
import helium314.keyboard.latin.BuildConfig
import helium314.keyboard.latin.R
import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.SubtypeLocaleUtils
Expand Down Expand Up @@ -181,12 +183,26 @@ object LocaleUtils {

@JvmStatic
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()
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") {
val resId = context.resources.getIdentifier("subtype_${languageTag.replace("-", "_")}", "string", context.packageName)
if (resId != 0) return context.getString(resId)
val resId = resources.getIdentifier(
"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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static String getSubtypeLocaleDisplayNameInternal(@NonNull final Locale
if (exceptionalNameResId != null) {
displayName = RunInLocaleKt.runInLocale(sResources, displayLocale, res -> res.getString(exceptionalNameResId));
} else {
displayName = locale.getDisplayName(displayLocale);
displayName = LocaleUtils.getLocaleDisplayNameInLocale(locale, sResources, displayLocale);
}
return StringUtils.capitalizeFirstCodePoint(displayName, displayLocale);
}
Expand Down

0 comments on commit 4476456

Please sign in to comment.