Skip to content

Commit 0908b24

Browse files
committed
chore: code cleanup
1 parent b490255 commit 0908b24

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

app/src/main/java/to/bitkit/models/Language.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ enum class Language(
4545
}
4646
}
4747

48+
fun Language.getLanguageTag(): String {
49+
return if (isSystemDefault) {
50+
""
51+
} else {
52+
if (countryCode != null) {
53+
"${languageCode}-${countryCode}"
54+
} else {
55+
languageCode
56+
}
57+
}
58+
}
59+
4860
fun Locale.toLanguage(): Language? {
4961
return Language.fromLocale(this)
5062
}

app/src/main/java/to/bitkit/ui/utils/AppLocaleManager.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.appcompat.app.AppCompatDelegate
88
import androidx.core.os.LocaleListCompat
99
import dagger.hilt.android.qualifiers.ApplicationContext
1010
import to.bitkit.models.Language
11+
import to.bitkit.models.getLanguageTag
1112
import to.bitkit.models.toLanguage
1213
import javax.inject.Inject
1314
import javax.inject.Singleton
@@ -17,26 +18,22 @@ class AppLocaleManager @Inject constructor(
1718
@ApplicationContext private val context: Context,
1819
) {
1920
fun changeLanguage(language: Language) {
20-
val languageTag = if (language.isSystemDefault) {
21-
// Clear application locales to use system default
22-
""
23-
} else {
24-
// Create language tag from language code and optional country code
25-
if (language.countryCode != null) {
26-
"${language.languageCode}-${language.countryCode}"
27-
} else {
28-
language.languageCode
29-
}
30-
}
21+
val languageTag = language.getLanguageTag()
3122

3223
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
3324
context.getSystemService(LocaleManager::class.java).applicationLocales =
34-
if (languageTag.isEmpty()) LocaleList.getEmptyLocaleList()
35-
else LocaleList.forLanguageTags(languageTag)
25+
if (languageTag.isEmpty()) {
26+
LocaleList.getEmptyLocaleList()
27+
} else {
28+
LocaleList.forLanguageTags(languageTag)
29+
}
3630
} else {
3731
AppCompatDelegate.setApplicationLocales(
38-
if (languageTag.isEmpty()) LocaleListCompat.getEmptyLocaleList()
39-
else LocaleListCompat.forLanguageTags(languageTag)
32+
if (languageTag.isEmpty()) {
33+
LocaleListCompat.getEmptyLocaleList()
34+
} else {
35+
LocaleListCompat.forLanguageTags(languageTag)
36+
}
4037
)
4138
}
4239
}

0 commit comments

Comments
 (0)