From 8c14628f21351dea7f0f1d666c5135d526ab72b2 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 13 Aug 2020 21:23:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BA=94=E7=94=A8=E5=86=85?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 2 +- .../io/legado/app/help/storage/Restore.kt | 2 +- .../app/ui/config/OtherConfigFragment.kt | 2 +- .../java/io/legado/app/utils/LanguageUtils.kt | 35 +++++++++++++------ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index a913a354c..1c51412d2 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -43,7 +43,7 @@ class App : MultiDexApplication() { super.onCreate() INSTANCE = this CrashHandler().init(this) - LanguageUtils.setConfiguration(this) + LanguageUtils.setConfigurationOld(this) db = AppDatabase.createDatabase(INSTANCE) packageManager.getPackageInfo(packageName, 0)?.let { versionCode = it.versionCode diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index ac63fe28b..77cfed255 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -188,7 +188,7 @@ object Restore { if (!BuildConfig.DEBUG) { LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon)) } - LanguageUtils.setConfiguration(App.INSTANCE) + LanguageUtils.setConfigurationOld(App.INSTANCE) App.INSTANCE.applyDayNight() postEvent(EventBus.SHOW_RSS, "") postEvent(EventBus.RECREATE, "") diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index e2836e688..628e0a8ee 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -118,7 +118,7 @@ class OtherConfigFragment : BasePreferenceFragment(), PreferKey.replaceEnableDefault -> AppConfig.replaceEnableDefault = App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true) PreferKey.language -> { - LanguageUtils.setConfiguration(App.INSTANCE) + LanguageUtils.setConfigurationOld(App.INSTANCE) postEvent(EventBus.RECREATE, "") } } diff --git a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt index 67afb05f2..3e297447c 100644 --- a/app/src/main/java/io/legado/app/utils/LanguageUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LanguageUtils.kt @@ -14,27 +14,42 @@ object LanguageUtils { * 设置语言 */ fun setConfiguration(context: Context): Context { - val resources: Resources = context.resources - val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { - "zh" -> Locale.CHINESE - "tw" -> Locale.TRADITIONAL_CHINESE - "en" -> Locale.ENGLISH - else -> getSystemLocale() - } - val configuration: Configuration = resources.configuration return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + val resources: Resources = context.resources + val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { + "zh" -> Locale.CHINESE + "tw" -> Locale.TRADITIONAL_CHINESE + "en" -> Locale.ENGLISH + else -> getSystemLocale() + } + val configuration: Configuration = resources.configuration configuration.setLocale(targetLocale) context.createConfigurationContext(configuration) } else { + context + } + } + + /** + * 设置语言 + */ + fun setConfigurationOld(context: Context) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + val resources: Resources = context.resources + val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { + "zh" -> Locale.CHINESE + "tw" -> Locale.TRADITIONAL_CHINESE + "en" -> Locale.ENGLISH + else -> getSystemLocale() + } + val configuration: Configuration = resources.configuration @Suppress("DEPRECATION") configuration.locale = targetLocale @Suppress("DEPRECATION") resources.updateConfiguration(configuration, resources.displayMetrics) - context } } - /** * 当前系统语言 */