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
This commit is contained in:
Levi Bard 2022-11-19 19:03:42 +01:00 committed by GitHub
parent ce1b9a53be
commit 3fb103aa14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -51,6 +51,7 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.os.LocaleListCompat
import androidx.core.view.ContentInfoCompat import androidx.core.view.ContentInfoCompat
import androidx.core.view.OnReceiveContentListener import androidx.core.view.OnReceiveContentListener
import androidx.core.view.isGone import androidx.core.view.isGone
@ -534,9 +535,28 @@ class ComposeActivity :
) )
} }
private fun mergeLocaleListCompat(list: MutableList<Locale>, 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?) { private fun setupLanguageSpinner(initialLanguage: String?) {
val locales = Locale.getAvailableLocales() val locales = mutableListOf<Locale>()
.filter { it.country.isNullOrEmpty() && it.script.isNullOrEmpty() && it.variant.isNullOrEmpty() } // Only "base" languages, "en" but not "en_DK" 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 } var currentLocaleIndex = locales.indexOfFirst { it.language == initialLanguage }
if (currentLocaleIndex < 0) { if (currentLocaleIndex < 0) {
Log.e(TAG, "Error looking up language tag '$initialLanguage', falling back to english") Log.e(TAG, "Error looking up language tag '$initialLanguage', falling back to english")