LibreTube/app/src/main/java/com/github/libretube/util/LocaleHelper.kt

142 lines
4.5 KiB
Kotlin
Raw Normal View History

2022-06-07 13:05:49 +05:30
package com.github.libretube.util
import android.content.Context
2022-11-06 14:32:45 +05:30
import android.content.res.Configuration
import android.content.res.Resources
2022-06-07 13:05:49 +05:30
import android.os.Build
2022-07-08 02:00:47 +05:30
import android.telephony.TelephonyManager
2022-09-08 21:59:00 +05:30
import com.github.libretube.constants.PreferenceKeys
2022-09-24 16:42:50 +05:30
import com.github.libretube.obj.Country
2022-06-07 13:05:49 +05:30
import java.util.*
2022-06-26 15:56:19 +05:30
object LocaleHelper {
2022-06-07 13:05:49 +05:30
fun updateLanguage(context: Context) {
2022-07-17 21:48:39 +05:30
val languageName = PreferenceHelper.getString(PreferenceKeys.LANGUAGE, "sys")
2022-11-06 14:32:45 +05:30
val locale = when {
languageName == "sys" -> Locale.getDefault()
languageName.contains("-") == true -> {
val languageParts = languageName.split("-")
Locale(
languageParts[0],
languageParts[1].replace("r", "")
)
}
else -> Locale(languageName)
2022-06-07 13:05:49 +05:30
}
2022-11-06 14:32:45 +05:30
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) updateResources(context, locale)
updateResourcesLegacy(context, locale)
2022-06-07 13:05:49 +05:30
}
2022-11-06 14:32:45 +05:30
private fun updateResources(context: Context, locale: Locale) {
2022-06-07 13:05:49 +05:30
Locale.setDefault(locale)
2022-11-06 14:32:45 +05:30
val configuration: Configuration = context.resources.configuration
configuration.setLocale(locale)
context.createConfigurationContext(configuration)
}
2022-06-07 13:05:49 +05:30
2022-11-06 14:32:45 +05:30
@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())
2022-06-07 13:05:49 +05:30
}
2022-07-08 02:00:47 +05:30
fun getDetectedCountry(context: Context, defaultCountryIsoCode: String): String {
detectSIMCountry(context)?.let {
2022-11-05 18:58:33 +05:30
if (it != "") return it
2022-07-08 02:00:47 +05:30
}
detectNetworkCountry(context)?.let {
2022-11-05 18:58:33 +05:30
if (it != "") return it
2022-07-08 02:00:47 +05:30
}
detectLocaleCountry(context)?.let {
2022-11-05 18:58:33 +05:30
if (it != "") return it
2022-07-08 02:00:47 +05:30
}
return defaultCountryIsoCode
}
private fun detectSIMCountry(context: Context): String? {
try {
2022-07-08 22:20:11 +05:30
val telephonyManager =
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
2022-07-08 02:00:47 +05:30
return telephonyManager.simCountryIso
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
private fun detectNetworkCountry(context: Context): String? {
try {
2022-07-08 22:20:11 +05:30
val telephonyManager =
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
2022-07-08 02:00:47 +05:30
return telephonyManager.networkCountryIso
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
private fun detectLocaleCountry(context: Context): String? {
try {
2022-09-24 16:42:50 +05:30
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.resources.configuration.locales[0].country
}
2022-07-08 02:00:47 +05:30
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
2022-09-24 16:42:50 +05:30
fun getAvailableCountries(): List<Country> {
val isoCountries = Locale.getISOCountries()
val countries = mutableListOf<Country>()
isoCountries.forEach { countryCode ->
val locale = Locale("", countryCode)
val countryName = locale.displayCountry
countries.add(
Country(
countryName,
countryCode
)
)
}
countries.sortBy { it.name }
return countries
}
2022-09-24 16:51:31 +05:30
fun getAvailableLocales(): List<Country> {
val availableLocales: Array<Locale> = Locale.getAvailableLocales()
val locales = mutableListOf<Country>()
availableLocales.forEach { locale ->
if (locales.filter { it.code == locale.language }.isEmpty()) {
locales.add(
Country(
locale.displayLanguage,
locale.language
)
)
}
}
return locales
}
2022-11-17 22:46:12 +05:30
fun getTrendingRegion(context: Context): String {
val regionPref = PreferenceHelper.getString(PreferenceKeys.REGION, "sys")
// get the system default country if auto region selected
return if (regionPref == "sys") {
getDetectedCountry(context, "UK")
.uppercase()
} else {
regionPref
}
}
2022-06-07 13:05:49 +05:30
}