添加应用内语言切换

pull/306/head
gedoor 4 years ago
parent 3552ced2ee
commit 8c14628f21
  1. 2
      app/src/main/java/io/legado/app/App.kt
  2. 2
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  3. 2
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  4. 21
      app/src/main/java/io/legado/app/utils/LanguageUtils.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

@ -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, "")

@ -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, "")
}
}

@ -14,6 +14,7 @@ object LanguageUtils {
* 设置语言
*/
fun setConfiguration(context: Context): Context {
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
@ -22,19 +23,33 @@ object LanguageUtils {
else -> getSystemLocale()
}
val configuration: Configuration = resources.configuration
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
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
}
}
/**
* 当前系统语言
*/

Loading…
Cancel
Save