mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
improved locale logic
This commit is contained in:
parent
2760257c26
commit
6059f877f1
@ -1,6 +1,8 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.os.Build
|
||||
import android.telephony.TelephonyManager
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
@ -11,32 +13,35 @@ object LocaleHelper {
|
||||
|
||||
fun updateLanguage(context: Context) {
|
||||
val languageName = PreferenceHelper.getString(PreferenceKeys.LANGUAGE, "sys")
|
||||
if (languageName == "sys") {
|
||||
updateLocaleConf(context, Locale.getDefault())
|
||||
} else if (languageName.contains("-") == true) {
|
||||
val languageParts = languageName.split("-")
|
||||
val locale = Locale(
|
||||
languageParts[0],
|
||||
languageParts[1]
|
||||
)
|
||||
updateLocaleConf(context, locale)
|
||||
} else {
|
||||
val locale = Locale(languageName)
|
||||
updateLocaleConf(context, locale)
|
||||
val locale = when {
|
||||
languageName == "sys" -> Locale.getDefault()
|
||||
languageName.contains("-") == true -> {
|
||||
val languageParts = languageName.split("-")
|
||||
Locale(
|
||||
languageParts[0],
|
||||
languageParts[1].replace("r", "")
|
||||
)
|
||||
}
|
||||
else -> Locale(languageName)
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) updateResources(context, locale)
|
||||
updateResourcesLegacy(context, locale)
|
||||
}
|
||||
|
||||
private fun updateLocaleConf(context: Context, locale: Locale) {
|
||||
// Change API Language
|
||||
private fun updateResources(context: Context, locale: Locale) {
|
||||
Locale.setDefault(locale)
|
||||
val configuration: Configuration = context.resources.configuration
|
||||
configuration.setLocale(locale)
|
||||
context.createConfigurationContext(configuration)
|
||||
}
|
||||
|
||||
// Change App Language
|
||||
val res = context.resources
|
||||
val dm = res.displayMetrics
|
||||
val conf = res.configuration
|
||||
conf.setLocale(locale)
|
||||
@Suppress("DEPRECATION")
|
||||
res.updateConfiguration(conf, dm)
|
||||
@Suppress("DEPRECATION")
|
||||
private fun updateResourcesLegacy(context: Context, locale: Locale) {
|
||||
Locale.setDefault(locale)
|
||||
val resources: Resources = context.resources
|
||||
val configuration: Configuration = resources.getConfiguration()
|
||||
configuration.locale = locale
|
||||
resources.updateConfiguration(configuration, resources.getDisplayMetrics())
|
||||
}
|
||||
|
||||
fun getDetectedCountry(context: Context, defaultCountryIsoCode: String): String {
|
||||
|
Loading…
x
Reference in New Issue
Block a user