From 3fb103aa1453732b856145e651a885423d4eee13 Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Sat, 19 Nov 2022 19:03:42 +0100 Subject: [PATCH] Prioritize users' default locales in language list (#2850) * Prioritize users' default locales in language list. Closes #2844 * Add the configured app languages before the configured system languages --- .../components/compose/ComposeActivity.kt | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index 20d0c483..d9f3b632 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -51,6 +51,7 @@ import androidx.appcompat.app.AppCompatDelegate import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.content.FileProvider +import androidx.core.os.LocaleListCompat import androidx.core.view.ContentInfoCompat import androidx.core.view.OnReceiveContentListener import androidx.core.view.isGone @@ -534,9 +535,28 @@ class ComposeActivity : ) } + private fun mergeLocaleListCompat(list: MutableList, localeListCompat: LocaleListCompat) { + for (index in 0 until localeListCompat.size()) { + val locale = localeListCompat[index] + if (locale != null && !list.contains(locale)) { + list.add(locale) + } + } + } + private fun setupLanguageSpinner(initialLanguage: String?) { - val locales = Locale.getAvailableLocales() - .filter { it.country.isNullOrEmpty() && it.script.isNullOrEmpty() && it.variant.isNullOrEmpty() } // Only "base" languages, "en" but not "en_DK" + val locales = mutableListOf() + mergeLocaleListCompat(locales, AppCompatDelegate.getApplicationLocales()) // configured app languages first + mergeLocaleListCompat(locales, LocaleListCompat.getDefault()) // then configured system languages + locales.addAll( // finally, other languages + // Only "base" languages, "en" but not "en_DK" + Locale.getAvailableLocales().filter { + it.country.isNullOrEmpty() && + it.script.isNullOrEmpty() && + it.variant.isNullOrEmpty() + } + ) + var currentLocaleIndex = locales.indexOfFirst { it.language == initialLanguage } if (currentLocaleIndex < 0) { Log.e(TAG, "Error looking up language tag '$initialLanguage', falling back to english")