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

40 lines
1.2 KiB
Kotlin
Raw Normal View History

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-06-26 01:51:22 +05:30
val languageName = PreferenceHelper.getString(context, "language", "en")
2022-06-07 13:05:49 +05:30
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)
}
}