添加应用内语言切换

pull/306/head
gedoor 4 years ago
parent b5ab558f8b
commit 7b93bf6e25
  1. 1
      app/src/main/assets/updateLog.md
  2. 4
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  3. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  4. 3
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  5. 51
      app/src/main/java/io/legado/app/utils/LanguageUtils.kt
  6. 7
      app/src/main/res/values-zh-rHK/arrays.xml
  7. 1
      app/src/main/res/values-zh-rHK/strings.xml
  8. 7
      app/src/main/res/values-zh-rTW/arrays.xml
  9. 1
      app/src/main/res/values-zh-rTW/strings.xml
  10. 7
      app/src/main/res/values-zh/arrays.xml
  11. 1
      app/src/main/res/values-zh/strings.xml
  12. 7
      app/src/main/res/values/array_values.xml
  13. 7
      app/src/main/res/values/arrays.xml
  14. 1
      app/src/main/res/values/strings.xml
  15. 7
      app/src/main/res/xml/pref_config_other.xml

@ -5,6 +5,7 @@
**2020/08/13**
* 添加亮度调节控件显示开关
* 添加应用内语言切换
**2020/08/12**
* 增加了Content Provider 接口支持 by [w568w](https://github.com/w568w)

@ -29,6 +29,10 @@ abstract class BaseActivity(
) : AppCompatActivity(),
CoroutineScope by MainScope() {
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(LanguageUtils.setConfiguration(newBase))
}
override fun onCreateView(
parent: View?,
name: String,

@ -2,6 +2,7 @@ package io.legado.app.constant
object PreferKey {
const val versionCode = "versionCode"
const val language = "language"
const val themeMode = "themeMode"
const val hideStatusBar = "hideStatusBar"
const val clickTurnPage = "clickTurnPage"

@ -117,6 +117,9 @@ class OtherConfigFragment : BasePreferenceFragment(),
)
PreferKey.replaceEnableDefault -> AppConfig.replaceEnableDefault =
App.INSTANCE.getPrefBoolean(PreferKey.replaceEnableDefault, true)
PreferKey.language -> {
postEvent(EventBus.RECREATE, "")
}
}
}

@ -0,0 +1,51 @@
package io.legado.app.utils
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import io.legado.app.constant.PreferKey
import java.util.*
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) {
configuration.setLocale(targetLocale)
context.createConfigurationContext(configuration)
} else {
@Suppress("DEPRECATION")
configuration.locale = targetLocale
@Suppress("DEPRECATION")
resources.updateConfiguration(configuration, resources.displayMetrics)
context
}
}
/**
* 当前系统语言
*/
private fun getSystemLocale(): Locale {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言
Resources.getSystem().configuration.locales.get(0)
} else {
@Suppress("DEPRECATION")
Resources.getSystem().configuration.locale
}
}
}

@ -64,4 +64,11 @@
<item>A屏黑</item>
</string-array>
<string-array name="language">
<item>跟随系统</item>
<item>简体中文</item>
<item>繁体中文</item>
<item>英文</item>
</string-array>
</resources>

@ -742,4 +742,5 @@
<string name="wrong_format">格式不对</string>
<string name="error">错误</string>
<string name="show_brightness_view">显示亮度调节控件</string>
<string name="language">语言</string>
</resources>

@ -121,4 +121,11 @@
<item>A屏黑</item>
</string-array>
<string-array name="language">
<item>跟随系统</item>
<item>简体中文</item>
<item>繁体中文</item>
<item>英文</item>
</string-array>
</resources>

@ -742,5 +742,6 @@
<string name="wrong_format">格式不对</string>
<string name="error">错误</string>
<string name="show_brightness_view">显示亮度调节控件</string>
<string name="language">语言</string>
</resources>

@ -121,4 +121,11 @@
<item>A屏黑</item>
</string-array>
<string-array name="language">
<item>跟随系统</item>
<item>简体中文</item>
<item>繁体中文</item>
<item>英文</item>
</string-array>
</resources>

@ -742,4 +742,5 @@
<string name="wrong_format">格式不对</string>
<string name="error">错误</string>
<string name="show_brightness_view">显示亮度调节控件</string>
<string name="language">语言</string>
</resources>

@ -48,4 +48,11 @@
<item>3</item>
</string-array>
<string-array name="language_value">
<item>auto</item>
<item>zh</item>
<item>tw</item>
<item>en</item>
</string-array>
</resources>

@ -121,4 +121,11 @@
<item>A屏黑</item>
</string-array>
<string-array name="language">
<item>跟随系统</item>
<item>简体中文</item>
<item>繁体中文</item>
<item>英文</item>
</string-array>
</resources>

@ -743,5 +743,6 @@
<string name="wrong_format">格式不对</string>
<string name="error">错误</string>
<string name="show_brightness_view">显示亮度调节控件</string>
<string name="language">语言</string>
</resources>

@ -2,6 +2,13 @@
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<io.legado.app.ui.widget.prefs.NameListPreference
android:key="language"
android:title="@string/language"
android:defaultValue="auto"
app:entries="@array/language"
app:entryValues="@array/language_value" />
<io.legado.app.ui.widget.prefs.PreferenceCategory
android:title="@string/main_activity"
app:allowDividerAbove="false"

Loading…
Cancel
Save