Skip to content

Commit 6d5cc0a

Browse files
committed
update locale utils and language preference
1 parent 96ec463 commit 6d5cc0a

3 files changed

Lines changed: 25 additions & 28 deletions

File tree

app/src/main/kotlin/com/vrem/util/LocaleUtils.kt

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ val baseSupportedLocales: List<Locale> =
6262
UKRAINIAN,
6363
).toList()
6464

65-
private const val SEPARATOR: String = "_"
66-
6765
fun findByCountryCode(countryCode: String): Locale =
6866
availableLocales.firstOrNull { countryCode.toCapitalize(Locale.getDefault()) == it.country }
6967
?: currentLocale
@@ -73,25 +71,19 @@ fun allCountries(): List<Locale> = countriesLocales.values.toList()
7371
fun supportedLanguages(): List<Locale> =
7472
(baseSupportedLocales + currentLocale).distinct()
7573

74+
fun supportedLanguageTags(): List<String> =
75+
listOf("") + baseSupportedLocales.map { it.toLanguageTag() }
76+
7677
fun findByLanguageTag(languageTag: String): Locale {
77-
val languageTagPredicate: (Locale) -> Boolean = {
78-
val locale: Locale = fromLanguageTag(languageTag)
78+
if (languageTag.isEmpty()) return currentLocale
79+
val locale = Locale.forLanguageTag(languageTag)
80+
return baseSupportedLocales.firstOrNull {
7981
it.language == locale.language && it.country == locale.country
80-
}
81-
return supportedLanguages().firstOrNull(languageTagPredicate) ?: currentLocale
82+
} ?: currentLocale
8283
}
8384

8485
fun currentCountryCode(): String = currentLocale.country
8586

86-
fun currentLanguageTag(): String = toLanguageTag(currentLocale)
87+
fun currentLanguageTag(): String = currentLocale.toLanguageTag()
8788

88-
fun toLanguageTag(locale: Locale): String = locale.language + SEPARATOR + locale.country
89-
90-
private fun fromLanguageTag(languageTag: String): Locale {
91-
val codes: Array<String> = languageTag.split(SEPARATOR).toTypedArray()
92-
return when (codes.size) {
93-
1 -> Locale.forLanguageTag(codes[0])
94-
2 -> Locale.forLanguageTag("${codes[0]}-${codes[1].toCapitalize(currentLocale)}")
95-
else -> currentLocale
96-
}
97-
}
89+
fun toLanguageTag(locale: Locale): String = locale.toLanguageTag()

app/src/main/kotlin/com/vrem/util/StringUtils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ fun String.Companion.nullToEmpty(value: String?): String = value ?: String.EMPTY
2727
fun String.specialTrim(): String = this.trim { it <= ' ' }.replace(" +".toRegex(), String.SPACE_SEPARATOR)
2828

2929
fun String.toCapitalize(locale: Locale): String = this.replaceFirstChar { word -> word.uppercase(locale) }
30+
31+
fun String.titlecaseFirst(locale: Locale): String =
32+
replaceFirstChar { it.titlecase(locale) }

app/src/main/kotlin/com/vrem/wifianalyzer/settings/LanguagePreference.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ package com.vrem.wifianalyzer.settings
1919

2020
import android.content.Context
2121
import android.util.AttributeSet
22-
import com.vrem.util.currentLanguageTag
23-
import com.vrem.util.supportedLanguages
24-
import com.vrem.util.toCapitalize
25-
import com.vrem.util.toLanguageTag
22+
import com.vrem.util.supportedLanguageTags
23+
import com.vrem.util.titlecaseFirst
24+
import com.vrem.wifianalyzer.R
2625
import java.util.Locale
2726

28-
private fun data(): List<Data> =
29-
supportedLanguages()
30-
.map { map(it) }
31-
.sorted()
32-
33-
private fun map(it: Locale): Data = Data(toLanguageTag(it), it.getDisplayName(it).toCapitalize(Locale.getDefault()))
27+
private fun data(context: Context): List<Data> =
28+
supportedLanguageTags().map { tag ->
29+
if (tag.isEmpty()) {
30+
Data("", context.getString(R.string.system_default))
31+
} else {
32+
val locale = Locale.forLanguageTag(tag)
33+
Data(tag, locale.getDisplayName(locale).titlecaseFirst(locale))
34+
}
35+
}
3436

3537
class LanguagePreference(
3638
context: Context,
3739
attrs: AttributeSet,
38-
) : CustomPreference(context, attrs, data(), currentLanguageTag())
40+
) : CustomPreference(context, attrs, data(context), "")

0 commit comments

Comments
 (0)