|
|
@ -4,6 +4,7 @@ import android.content.Context |
|
|
|
import android.content.res.Configuration |
|
|
|
import android.content.res.Configuration |
|
|
|
import android.content.res.Resources |
|
|
|
import android.content.res.Resources |
|
|
|
import android.os.Build |
|
|
|
import android.os.Build |
|
|
|
|
|
|
|
import android.os.LocaleList |
|
|
|
import io.legado.app.constant.PreferKey |
|
|
|
import io.legado.app.constant.PreferKey |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
@ -16,14 +17,14 @@ object LanguageUtils { |
|
|
|
fun setConfiguration(context: Context): Context { |
|
|
|
fun setConfiguration(context: Context): Context { |
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
val resources: Resources = context.resources |
|
|
|
val resources: Resources = context.resources |
|
|
|
val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { |
|
|
|
val metrics = resources.displayMetrics |
|
|
|
"zh" -> Locale.CHINESE |
|
|
|
|
|
|
|
"tw" -> Locale.TRADITIONAL_CHINESE |
|
|
|
|
|
|
|
"en" -> Locale.ENGLISH |
|
|
|
|
|
|
|
else -> getSystemLocale() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
val configuration: Configuration = resources.configuration |
|
|
|
val configuration: Configuration = resources.configuration |
|
|
|
|
|
|
|
//Log.d("h11128", "set language to ${context.getPrefString(PreferKey.language)}") |
|
|
|
|
|
|
|
val targetLocale = getSetLocale(context) |
|
|
|
configuration.setLocale(targetLocale) |
|
|
|
configuration.setLocale(targetLocale) |
|
|
|
|
|
|
|
configuration.setLocales(LocaleList(targetLocale)) |
|
|
|
|
|
|
|
@Suppress("DEPRECATION") |
|
|
|
|
|
|
|
resources.updateConfiguration(configuration, metrics) |
|
|
|
context.createConfigurationContext(configuration) |
|
|
|
context.createConfigurationContext(configuration) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
setConfigurationOld(context) |
|
|
|
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) { |
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { |
|
|
|
val resources: Resources = context.resources |
|
|
|
val resources: Resources = context.resources |
|
|
|
val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { |
|
|
|
val targetLocale = getSetLocale(context) |
|
|
|
"zh" -> Locale.CHINESE |
|
|
|
|
|
|
|
"tw" -> Locale.TRADITIONAL_CHINESE |
|
|
|
|
|
|
|
"en" -> Locale.ENGLISH |
|
|
|
|
|
|
|
else -> getSystemLocale() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
val configuration: Configuration = resources.configuration |
|
|
|
val configuration: Configuration = resources.configuration |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
configuration.locale = targetLocale |
|
|
|
configuration.locale = targetLocale |
|
|
@ -55,13 +51,62 @@ object LanguageUtils { |
|
|
|
* 当前系统语言 |
|
|
|
* 当前系统语言 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun getSystemLocale(): Locale { |
|
|
|
private fun getSystemLocale(): Locale { |
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 |
|
|
|
val locale: Locale |
|
|
|
Resources.getSystem().configuration.locales.get(0) |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 |
|
|
|
|
|
|
|
locale = Resources.getSystem().configuration.locales.get(0) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
@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 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |