mirror of
https://github.com/mihonapp/mihon.git
synced 2024-12-25 02:18:24 +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()
|
setupAcra()
|
||||||
setupJobManager()
|
setupJobManager()
|
||||||
|
|
||||||
LocaleHelper.updateCfg(this, baseContext.resources.configuration)
|
LocaleHelper.updateConfiguration(this, resources.configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun attachBaseContext(base: Context) {
|
override fun attachBaseContext(base: Context) {
|
||||||
@ -46,7 +46,7 @@ open class App : Application() {
|
|||||||
|
|
||||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||||
super.onConfigurationChanged(newConfig)
|
super.onConfigurationChanged(newConfig)
|
||||||
LocaleHelper.updateCfg(this, newConfig)
|
LocaleHelper.updateConfiguration(this, newConfig, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun setupAcra() {
|
protected open fun setupAcra() {
|
||||||
|
@ -6,7 +6,7 @@ import eu.kanade.tachiyomi.util.LocaleHelper
|
|||||||
abstract class BaseActivity : AppCompatActivity(), ActivityMixin {
|
abstract class BaseActivity : AppCompatActivity(), ActivityMixin {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
LocaleHelper.updateCfg(this)
|
LocaleHelper.updateConfiguration(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getActivity() = this
|
override fun getActivity() = this
|
||||||
|
@ -9,7 +9,7 @@ import nucleus.view.NucleusAppCompatActivity
|
|||||||
abstract class BaseRxActivity<P : BasePresenter<*>> : NucleusAppCompatActivity<P>(), ActivityMixin {
|
abstract class BaseRxActivity<P : BasePresenter<*>> : NucleusAppCompatActivity<P>(), ActivityMixin {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
LocaleHelper.updateCfg(this)
|
LocaleHelper.updateConfiguration(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate(savedState: Bundle?) {
|
override fun onCreate(savedState: Bundle?) {
|
||||||
|
@ -115,7 +115,8 @@ class SettingsGeneralFragment : SettingsFragment(),
|
|||||||
langPreference.setOnPreferenceChangeListener { preference, newValue ->
|
langPreference.setOnPreferenceChangeListener { preference, newValue ->
|
||||||
(activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_LANG_CHANGED
|
(activity as SettingsActivity).parentFlags = SettingsActivity.FLAG_LANG_CHANGED
|
||||||
LocaleHelper.changeLocale(newValue.toString())
|
LocaleHelper.changeLocale(newValue.toString())
|
||||||
LocaleHelper.updateCfg(activity.application, activity.baseContext.resources.configuration)
|
val app = activity.application
|
||||||
|
LocaleHelper.updateConfiguration(app, app.resources.configuration)
|
||||||
activity.recreate()
|
activity.recreate()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.util
|
|||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.os.LocaleList
|
||||||
import android.view.ContextThemeWrapper
|
import android.view.ContextThemeWrapper
|
||||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||||
import uy.kohesive.injekt.injectLazy
|
import uy.kohesive.injekt.injectLazy
|
||||||
@ -20,12 +21,9 @@ object LocaleHelper {
|
|||||||
private val preferences: PreferencesHelper by injectLazy()
|
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 system's locale.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
private var v16SystemLocale = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
|
private var systemLocale: Locale? = null
|
||||||
preferences.context.resources.configuration.locale else null
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The application's locale. When it's null, the system locale is used.
|
* 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) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && appLocale != null) {
|
||||||
val config = Configuration(preferences.context.resources.configuration)
|
val config = Configuration(preferences.context.resources.configuration)
|
||||||
config.setLocale(appLocale)
|
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) {
|
fun updateConfiguration(app: Application, config: Configuration, configChange: Boolean = false) {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
if (systemLocale == null) {
|
||||||
val configCopy = Configuration(config)
|
systemLocale = getConfigLocale(config)
|
||||||
val displayMetrics = app.baseContext.resources.displayMetrics
|
}
|
||||||
configCopy.locale = appLocale ?: v16SystemLocale
|
// In API 16 and lower the system locale can't be changed.
|
||||||
app.baseContext.resources.updateConfiguration(configCopy, displayMetrics)
|
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