@@ -2,21 +2,39 @@ package com.codee.app.core.extensions
2
2
3
3
import androidx.core.os.ConfigurationCompat
4
4
import com.codee.app.core.ContextDI.context
5
+ import com.codee.app.resources.locale.Locale
5
6
import com.codee.app.resources.locale.Localized
6
7
import com.codee.app.resources.locale.get
7
8
9
+
10
+ /* *
11
+ * First it checks for full equality (languageCode and countryCode), then if there is no such a
12
+ * string, it searches for string with the same languageCode but without countryCode specified.
13
+ * Then the it tries to get just any string with the languageCode
14
+ *
15
+ * It repeats with all available locales and finally uses default variant as fallback
16
+ */
8
17
fun <T > Localized<T>.localize (): T {
9
- val locale = ConfigurationCompat
10
- .getLocales(context.resources.configuration)[0 ]
11
- .toCodeeLocale()
18
+ // getLocales returns custom class, not list
19
+ val locales = mutableListOf<Locale >().apply {
20
+ val list = ConfigurationCompat .getLocales(context.resources.configuration)
21
+ for (i in 1 .. list.size())
22
+ add(list[i].toCodeeLocale())
23
+ }
12
24
13
- val variants = variants.toList()
25
+ for (locale in locales) {
26
+ val languageCodeMatches = variants
27
+ .filter { (k) -> k.languageCode == locale.languageCode }
28
+ val countryCodeMatches = languageCodeMatches
29
+ .filter { (k) -> k.countryCode == locale.countryCode }
14
30
15
- val withLanguageCode = variants
16
- .filter { (k) -> k.languageCode == locale.languageCode }
31
+ val result = countryCodeMatches.values.firstOrNull()
32
+ ? : languageCodeMatches.filter { (k) -> k.countryCode == null }.values.firstOrNull()
33
+ ? : languageCodeMatches.values.firstOrNull()
17
34
18
- val withCountryCode = withLanguageCode
19
- .firstOrNull { (k) -> k.countryCode == locale.countryCode }
35
+ if (result != null )
36
+ return result
37
+ }
20
38
21
- return (withCountryCode ? : withLanguageCode.firstOrNull())?.second ? : default
22
- }
39
+ return default
40
+ }
0 commit comments