Merge pull request #414 from h11128/h11128pull

语言切换bug修复
pull/416/head
kunfei 4 years ago committed by GitHub
commit 32cd813fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/src/main/java/io/legado/app/App.kt
  2. 1
      app/src/main/java/io/legado/app/constant/EventBus.kt
  3. 4
      app/src/main/java/io/legado/app/help/ActivityHelp.kt
  4. 2
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  5. 10
      app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt
  6. 6
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  7. 77
      app/src/main/java/io/legado/app/utils/LanguageUtils.kt
  8. 8
      app/src/main/res/values/arrays.xml
  9. 1
      app/src/main/res/values/strings.xml

@ -7,6 +7,7 @@ import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.provider.Settings
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDexApplication
@ -46,7 +47,7 @@ class App : MultiDexApplication() {
INSTANCE = this
androidId = Settings.System.getString(contentResolver, Settings.Secure.ANDROID_ID)
CrashHandler(this)
LanguageUtils.setConfigurationOld(this)
LanguageUtils.setConfiguration(this)
db = AppDatabase.createDatabase(INSTANCE)
packageManager.getPackageInfo(packageName, 0)?.let {
versionCode = it.versionCode

@ -3,6 +3,7 @@ package io.legado.app.constant
object EventBus {
const val MEDIA_BUTTON = "mediaButton"
const val RECREATE = "RECREATE"
const val REOPEN = "REOPEN"
const val UP_BOOK = "upBookToc"
const val ALOUD_STATE = "aloud_state"
const val TTS_PROGRESS = "ttsStart"

@ -3,6 +3,7 @@ package io.legado.app.help
import android.app.Activity
import android.app.Application
import android.os.Bundle
import io.legado.app.utils.LanguageUtils
import java.lang.ref.WeakReference
import java.util.*
@ -110,5 +111,8 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
add(activity)
if (!LanguageUtils.isSameWithSetting(activity)){
LanguageUtils.setConfiguration(activity)
}
}
}

@ -225,7 +225,7 @@ object Restore {
if (!BuildConfig.DEBUG) {
LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon))
}
LanguageUtils.setConfigurationOld(App.INSTANCE)
LanguageUtils.setConfiguration(App.INSTANCE)
App.INSTANCE.applyDayNight()
postEvent(EventBus.SHOW_RSS, "")
postEvent(EventBus.RECREATE, "")

@ -1,9 +1,13 @@
package io.legado.app.ui.config
import android.content.Intent
import android.os.Bundle
import android.os.Process
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.EventBus
import io.legado.app.ui.main.MainActivity
import io.legado.app.utils.getViewModel
import io.legado.app.utils.observeEvent
import kotlinx.android.synthetic.main.activity_config.*
@ -54,5 +58,11 @@ class ConfigActivity : VMBaseActivity<ConfigViewModel>(R.layout.activity_config)
observeEvent<String>(EventBus.RECREATE) {
recreate()
}
observeEvent<String>(EventBus.REOPEN) {
val intent = Intent(App.INSTANCE, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
App.INSTANCE.startActivity(intent)
Process.killProcess(Process.myPid())
}
}
}

@ -7,6 +7,7 @@ import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.documentfile.provider.DocumentFile
import androidx.preference.ListPreference
@ -31,6 +32,7 @@ import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.ui.widget.number.NumberPickerDialog
import io.legado.app.utils.*
import java.io.File
import java.util.*
class OtherConfigFragment : BasePreferenceFragment(),
@ -123,8 +125,8 @@ class OtherConfigFragment : BasePreferenceFragment(),
PreferKey.replaceEnableDefault -> AppConfig.replaceEnableDefault =
App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true)
PreferKey.language -> {
LanguageUtils.setConfigurationOld(App.INSTANCE)
postEvent(EventBus.RECREATE, "")
LanguageUtils.setConfiguration(App.INSTANCE)
postEvent(EventBus.REOPEN, "")
}
}
}

@ -4,6 +4,7 @@ import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.LocaleList
import io.legado.app.constant.PreferKey
import java.util.*
@ -16,14 +17,14 @@ 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
"tw" -> Locale.TRADITIONAL_CHINESE
"en" -> Locale.ENGLISH
else -> getSystemLocale()
}
val metrics = resources.displayMetrics
val configuration: Configuration = resources.configuration
//Log.d("h11128", "set language to ${context.getPrefString(PreferKey.language)}")
val targetLocale = getSetLocale(context)
configuration.setLocale(targetLocale)
configuration.setLocales(LocaleList(targetLocale))
@Suppress("DEPRECATION")
resources.updateConfiguration(configuration, metrics)
context.createConfigurationContext(configuration)
} else {
setConfigurationOld(context)
@ -34,15 +35,10 @@ object LanguageUtils {
/**
* 设置语言
*/
fun setConfigurationOld(context: Context) {
private 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 targetLocale = getSetLocale(context)
val configuration: Configuration = resources.configuration
@Suppress("DEPRECATION")
configuration.locale = targetLocale
@ -55,13 +51,62 @@ object LanguageUtils {
* 当前系统语言
*/
private fun getSystemLocale(): Locale {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言
Resources.getSystem().configuration.locales.get(0)
val locale: Locale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言
locale = Resources.getSystem().configuration.locales.get(0)
} else {
@Suppress("DEPRECATION")
Resources.getSystem().configuration.locale
locale = Resources.getSystem().configuration.locale
}
return locale
}
/**
* 当前App语言
*/
private fun getAppLocale(context: Context): Locale {
val locale: Locale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = context.resources.configuration.locales[0]
} else {
@Suppress("DEPRECATION")
locale = context.resources.configuration.locale
}
/*
Log.d("h11128", "displayName " + locale.displayName)
Log.d("h11128", "displayCountry " + locale.displayCountry)
Log.d("h11128", "displayLanguage " + locale.displayLanguage)
Log.d("h11128", "Language " + locale.language)
Log.d("h11128", "Country " + locale.country)
*/
return locale
}
/**
* 当前设置语言
*/
private fun getSetLocale(context: Context): Locale {
return when (context.getPrefString(PreferKey.language)) {
"zh" -> Locale.SIMPLIFIED_CHINESE
"tw" -> Locale.TRADITIONAL_CHINESE
"en" -> Locale.ENGLISH
else -> getSystemLocale()
}
}
/**
* 判断App语言和设置语言是否相同
*/
fun isSameWithSetting(context: Context): Boolean {
val locale = getAppLocale(context)
val language = locale.language
val country = locale.country
val pfLocale = getSetLocale(context)
val pfLanguage = pfLocale.language
val pfCountry = pfLocale.country
return language == pfLanguage && country == pfCountry
}
}

@ -99,10 +99,10 @@
</string-array>
<string-array name="language">
<item>跟随系统</item>
<item>简体中文</item>
<item>繁体中文</item>
<item>英文</item>
<item>Auto</item>
<item>Simplified_Chinese</item>
<item>Traditional_Chinese</item>
<item>English</item>
</string-array>
</resources>

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--App-->
<string name="app_name">Legado</string>

Loading…
Cancel
Save