@@ -67,51 +67,39 @@ fun Context.getLocaleListFromXml(): LocaleListCompat {
6767 * For preference drop down convenience.
6868 */
6969fun Context.getLangPreferenceDropdownEntries (): Map <String , Locale > {
70- val localeList = getLocaleListFromXml()
71- val currentLocaleList: List <Locale > =
72- (
73- if (! AppCompatDelegate .getApplicationLocales().isEmpty) {
74- AppCompatDelegate .getApplicationLocales()
75- } else {
76- LocaleListCompat .getDefault()
77- }
78- ).let { appLocales ->
79- ArrayList <Locale >().apply {
80- for (x in 0 until appLocales.size()) {
81- appLocales.get(x)?.let {
82- this .add(it)
83- }
84- }
85- }
70+ val appLocales =
71+ if (AppCompatDelegate .getApplicationLocales().isEmpty) {
72+ LocaleListCompat .getDefault()
73+ } else {
74+ AppCompatDelegate .getApplicationLocales()
8675 }
76+
77+ val currentLocaleList = ArrayList <Locale >()
78+
79+ for (i in 0 until appLocales.size()) {
80+ val appLocale = appLocales.get(i) ? : continue
81+ currentLocaleList.add(appLocale)
82+ }
83+
84+ val xmlLocales = getLocaleListFromXml()
8785 val map = mutableMapOf<String , Locale >()
8886
89- for (a in 0 until localeList.size()) {
90- localeList[a].let {
91- it?.run {
92- val displayName: String =
93- if (currentLocaleList.isEmpty()) {
94- this .getDisplayName(Locale .getDefault())
95- } else {
96- this .getDisplayName(
97- currentLocaleList.first { locale ->
98- this .getDisplayName(locale).isNotEmpty()
99- },
100- )
87+ for (i in 0 until xmlLocales.size()) {
88+ val xmlLocale = xmlLocales[i] ? : continue
89+ val displayName: String =
90+ if (currentLocaleList.isEmpty()) {
91+ xmlLocale.getDisplayName(Locale .getDefault())
92+ } else {
93+ val nameInCurrentLocale =
94+ currentLocaleList.first { locale ->
95+ xmlLocale.getDisplayName(locale).isNotEmpty()
10196 }
102- map.put(displayName, this )
97+
98+ xmlLocale.getDisplayName(nameInCurrentLocale)
10399 }
104- }
100+
101+ map[displayName] = xmlLocale
105102 }
106- return map
107- }
108103
109- /* *
110- * [Context] extension to set app locale fluently.
111- *
112- * Calls [AppCompatDelegate.setApplicationLocales] under the hood.
113- */
114- fun Context.setLocale (langTag : String ) {
115- val appLocale: LocaleListCompat = LocaleListCompat .forLanguageTags(langTag)
116- AppCompatDelegate .setApplicationLocales(appLocale)
104+ return map
117105}
0 commit comments