2022-06-07 13:05:49 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.os.Build
|
2022-07-02 21:53:24 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceHelper
|
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-06 19:43:51 +05:30
|
|
|
val languageName = PreferenceHelper.getString(context, "language", "sys")
|
|
|
|
if (languageName == "sys") updateLocaleConf(context, Locale.getDefault())
|
|
|
|
else if ("$languageName".length < 3) {
|
|
|
|
val locale = Locale(languageName.toString())
|
|
|
|
updateLocaleConf(context, locale)
|
|
|
|
} else if ("$languageName".length > 3) {
|
|
|
|
val locale = Locale(
|
|
|
|
languageName?.substring(0, 2).toString(),
|
|
|
|
languageName?.substring(4, 6).toString()
|
|
|
|
)
|
|
|
|
updateLocaleConf(context, locale)
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 19:43:51 +05:30
|
|
|
private fun updateLocaleConf(context: Context, locale: Locale) {
|
2022-06-07 13:05:49 +05:30
|
|
|
// Change API Language
|
|
|
|
Locale.setDefault(locale)
|
|
|
|
|
|
|
|
// Change App Language
|
|
|
|
val res = context.resources
|
|
|
|
val dm = res.displayMetrics
|
|
|
|
val conf = res.configuration
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
|
|
|
conf.setLocale(locale)
|
|
|
|
} else {
|
|
|
|
conf.locale = locale
|
|
|
|
}
|
|
|
|
res.updateConfiguration(conf, dm)
|
|
|
|
}
|
|
|
|
}
|