mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-12-22 22:01:51 +01:00
Fix locale not applied outside activities
This commit is contained in:
parent
1a3a1db4ff
commit
006d17aac7
@ -34,7 +34,7 @@ open class App : Application() {
|
||||
setupAcra()
|
||||
setupJobManager()
|
||||
|
||||
LocaleHelper.updateCfg(this, baseContext.resources.configuration)
|
||||
LocaleHelper.updateConfiguration(this, resources.configuration)
|
||||
}
|
||||
|
||||
override fun attachBaseContext(base: Context) {
|
||||
@ -46,7 +46,7 @@ open class App : Application() {
|
||||
|
||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||
super.onConfigurationChanged(newConfig)
|
||||
LocaleHelper.updateCfg(this, newConfig)
|
||||
LocaleHelper.updateConfiguration(this, newConfig, true)
|
||||
}
|
||||
|
||||
protected open fun setupAcra() {
|
||||
|
@ -6,7 +6,7 @@ import eu.kanade.tachiyomi.util.LocaleHelper
|
||||
abstract class BaseActivity : AppCompatActivity(), ActivityMixin {
|
||||
|
||||
init {
|
||||
LocaleHelper.updateCfg(this)
|
||||
LocaleHelper.updateConfiguration(this)
|
||||
}
|
||||
|
||||
override fun getActivity() = this
|
||||
|
@ -9,7 +9,7 @@ import nucleus.view.NucleusAppCompatActivity
|
||||
abstract class BaseRxActivity<P : BasePresenter<*>> : NucleusAppCompatActivity<P>(), ActivityMixin {
|
||||
|
||||
init {
|
||||
LocaleHelper.updateCfg(this)
|
||||
LocaleHelper.updateConfiguration(this)
|
||||
}
|
||||
|
||||
override fun onCreate(savedState: Bundle?) {
|
||||
|
@ -115,7 +115,8 @@ class SettingsGeneralFragment : SettingsFragment(),
|
||||
langPreference.setOnPreferenceChangeListener { preference, newValue ->
|
||||
(activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_LANG_CHANGED
|
||||
LocaleHelper.changeLocale(newValue.toString())
|
||||
LocaleHelper.updateCfg(activity.application, activity.baseContext.resources.configuration)
|
||||
val app = activity.application
|
||||
LocaleHelper.updateConfiguration(app, app.resources.configuration)
|
||||
activity.recreate()
|
||||
true
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.util
|
||||
import android.app.Application
|
||||
import android.content.res.Configuration
|
||||
import android.os.Build
|
||||
import android.os.LocaleList
|
||||
import android.view.ContextThemeWrapper
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
@ -20,12 +21,9 @@ object LocaleHelper {
|
||||
private val preferences: PreferencesHelper by injectLazy()
|
||||
|
||||
/**
|
||||
* In API 16 and below the application's configuration has to be changed, so we need a copy of
|
||||
* the initial locale. The only problem is that if the system locale changes while the app is
|
||||
* running, it won't change until an application restart.
|
||||
* The system's locale.
|
||||
*/
|
||||
private var v16SystemLocale = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||
preferences.context.resources.configuration.locale else null
|
||||
private var systemLocale: Locale? = null
|
||||
|
||||
/**
|
||||
* The application's locale. When it's null, the system locale is used.
|
||||
@ -57,9 +55,9 @@ object LocaleHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the app's language from API 17.
|
||||
* Updates the app's language to an activity.
|
||||
*/
|
||||
fun updateCfg(wrapper: ContextThemeWrapper) {
|
||||
fun updateConfiguration(wrapper: ContextThemeWrapper) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && appLocale != null) {
|
||||
val config = Configuration(preferences.context.resources.configuration)
|
||||
config.setLocale(appLocale)
|
||||
@ -68,16 +66,36 @@ object LocaleHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the app's language for API 16 and lower.
|
||||
* Updates the app's language to the application.
|
||||
*/
|
||||
fun updateCfg(app: Application, config: Configuration) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
val configCopy = Configuration(config)
|
||||
val displayMetrics = app.baseContext.resources.displayMetrics
|
||||
configCopy.locale = appLocale ?: v16SystemLocale
|
||||
app.baseContext.resources.updateConfiguration(configCopy, displayMetrics)
|
||||
fun updateConfiguration(app: Application, config: Configuration, configChange: Boolean = false) {
|
||||
if (systemLocale == null) {
|
||||
systemLocale = getConfigLocale(config)
|
||||
}
|
||||
// In API 16 and lower the system locale can't be changed.
|
||||
if (configChange && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
val newLocale = getConfigLocale(config)
|
||||
if (systemLocale == newLocale) {
|
||||
return
|
||||
}
|
||||
systemLocale = newLocale
|
||||
}
|
||||
val newConfig = Configuration(config)
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||
newConfig.locale = appLocale ?: systemLocale
|
||||
} else {
|
||||
newConfig.locales = LocaleList(appLocale ?: systemLocale)
|
||||
}
|
||||
val resources = app.resources
|
||||
resources.updateConfiguration(newConfig, resources.displayMetrics)
|
||||
}
|
||||
|
||||
private fun getConfigLocale(config: Configuration): Locale {
|
||||
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||
config.locale
|
||||
} else {
|
||||
config.locales[0]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user