2016-12-13 14:47:46 -05:00
|
|
|
package eu.kanade.tachiyomi.util
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import android.content.res.Configuration
|
|
|
|
import android.os.Build
|
|
|
|
import android.view.ContextThemeWrapper
|
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
|
|
|
import uy.kohesive.injekt.injectLazy
|
|
|
|
import java.util.Locale
|
|
|
|
|
|
|
|
object LocaleHelper {
|
|
|
|
|
|
|
|
private val preferences: PreferencesHelper by injectLazy()
|
2016-12-13 15:07:48 -05:00
|
|
|
|
|
|
|
private var pLocale = Locale(intToLangCode(preferences.lang()))
|
2016-12-13 14:47:46 -05:00
|
|
|
|
|
|
|
fun setLocale(locale: Locale) {
|
|
|
|
pLocale = locale
|
|
|
|
Locale.setDefault(pLocale)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun updateCfg(wrapper: ContextThemeWrapper) {
|
2016-12-13 15:07:48 -05:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
2016-12-18 16:31:20 -05:00
|
|
|
val config = Configuration(preferences.context.resources.configuration)
|
2016-12-13 14:47:46 -05:00
|
|
|
config.setLocale(pLocale)
|
|
|
|
wrapper.applyOverrideConfiguration(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 12:51:12 -05:00
|
|
|
fun updateCfg(app: Application, config: Configuration) {
|
2016-12-13 15:07:48 -05:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
2016-12-13 14:47:46 -05:00
|
|
|
config.locale = pLocale
|
|
|
|
app.baseContext.resources.updateConfiguration(config, app.baseContext.resources.displayMetrics)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun intToLangCode(i: Int): String {
|
2016-12-13 15:07:48 -05:00
|
|
|
return when(i) {
|
2016-12-13 14:47:46 -05:00
|
|
|
1 -> "en"
|
|
|
|
2 -> "es"
|
|
|
|
3 -> "it"
|
|
|
|
4 -> "pt"
|
2016-12-13 15:07:48 -05:00
|
|
|
else -> "" // System Language
|
2016-12-13 14:47:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|