Implement language switcher (#563)

* Implement language switching using BaseActivity

* Add requested changes

* Cleanup App.kt Imports and add pref_language_key

* Acutally use @string for key

* Use string resource for language preference title
This commit is contained in:
Ken Swenson
2016-12-13 14:47:46 -05:00
committed by inorichi
parent 4061c7450b
commit cc9fd53abb
12 changed files with 114 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
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()
private var pLocale = Locale(LocaleHelper.intToLangCode(preferences.lang()))
fun setLocale(locale: Locale) {
pLocale = locale
Locale.setDefault(pLocale)
}
fun updateCfg(wrapper: ContextThemeWrapper) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
val config = Configuration()
config.setLocale(pLocale)
wrapper.applyOverrideConfiguration(config)
}
}
fun updateCfg(app: Application, config: Configuration){
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.locale = pLocale
app.baseContext.resources.updateConfiguration(config, app.baseContext.resources.displayMetrics)
}
}
fun intToLangCode(i: Int): String {
return when(i){
1 -> "en"
2 -> "es"
3 -> "it"
4 -> "pt"
// System Language
else -> ""
}
}
}