LibreTube/app/src/main/java/com/github/libretube/util/LocaleHelper.kt
2022-06-25 22:21:22 +02:00

39 lines
1.1 KiB
Kotlin

package com.github.libretube.util
import android.content.Context
import android.os.Build
import java.util.*
class LocaleHelper {
fun updateLanguage(context: Context) {
val languageName = PreferenceHelper.getString(context, "language", "en")
if (languageName != "") {
setLanguage(context, languageName!!)
}
}
private fun setLanguage(context: Context, languageName: String) {
val locale = if (languageName != "sys" && "$languageName".length < 3) {
Locale(languageName)
} else if ("$languageName".length > 3) {
Locale(languageName?.substring(0, 2), languageName?.substring(4, 6))
} else {
Locale.getDefault()
}
// 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)
}
}