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

92 lines
3.2 KiB
Kotlin
Raw Normal View History

package com.github.libretube.helpers
2022-06-07 13:05:49 +05:30
import android.content.Context
2022-11-06 14:32:45 +05:30
import android.content.res.Configuration
2022-06-07 13:05:49 +05:30
import android.os.Build
2022-07-08 02:00:47 +05:30
import android.telephony.TelephonyManager
2023-01-14 15:18:27 +05:30
import androidx.core.content.getSystemService
import androidx.core.os.ConfigurationCompat
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()
2023-01-14 15:26:38 +05:30
languageName.contains("-") -> {
2022-11-06 14:32:45 +05:30
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)
2023-01-16 12:14:30 +05:30
val resources = context.resources
2023-01-14 15:26:38 +05:30
val configuration = resources.configuration
2022-11-06 14:32:45 +05:30
configuration.locale = locale
2023-01-14 15:26:38 +05:30
resources.updateConfiguration(configuration, resources.displayMetrics)
2022-06-07 13:05:49 +05:30
}
2022-07-08 02:00:47 +05:30
2023-01-14 15:18:27 +05:30
private fun getDetectedCountry(context: Context): String {
2023-01-16 12:14:30 +05:30
return detectSIMCountry(context)
?: detectNetworkCountry(context)
?: detectLocaleCountry(context)
?: "UK"
2022-07-08 02:00:47 +05:30
}
2023-01-16 12:14:30 +05:30
private fun detectSIMCountry(context: Context): String? {
return context.getSystemService<TelephonyManager>()?.simCountryIso?.ifEmpty { null }
2022-07-08 02:00:47 +05:30
}
2023-01-16 12:14:30 +05:30
private fun detectNetworkCountry(context: Context): String? {
return context.getSystemService<TelephonyManager>()?.networkCountryIso?.ifEmpty { null }
2022-07-08 02:00:47 +05:30
}
2023-01-16 12:14:30 +05:30
private fun detectLocaleCountry(context: Context): String? {
2023-01-14 15:18:27 +05:30
return ConfigurationCompat.getLocales(context.resources.configuration)[0]!!.country
2023-01-16 12:14:30 +05:30
.ifEmpty { null }
2022-07-08 02:00:47 +05:30
}
2022-09-24 16:42:50 +05:30
fun getAvailableCountries(): List<Country> {
2023-01-14 15:35:32 +05:30
return Locale.getISOCountries()
.map { Country(Locale("", it).displayCountry, it) }
.sortedBy { it.name }
2022-09-24 16:42:50 +05:30
}
2022-09-24 16:51:31 +05:30
fun getAvailableLocales(): List<Country> {
2023-01-14 15:35:32 +05:30
return Locale.getAvailableLocales()
.distinctBy { it.language }
.map { Country(it.displayLanguage, it.language) }
.sortedBy { it.name }
2022-09-24 16:51:31 +05:30
}
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") {
2023-01-14 15:18:27 +05:30
getDetectedCountry(context).uppercase()
2022-11-17 22:46:12 +05:30
} else {
regionPref
}
}
2022-06-07 13:05:49 +05:30
}