|
|
@ -14,27 +14,42 @@ object LanguageUtils { |
|
|
|
* 设置语言 |
|
|
|
* 设置语言 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun setConfiguration(context: Context): Context { |
|
|
|
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) { |
|
|
|
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) |
|
|
|
configuration.setLocale(targetLocale) |
|
|
|
context.createConfigurationContext(configuration) |
|
|
|
context.createConfigurationContext(configuration) |
|
|
|
} else { |
|
|
|
} 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") |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
configuration.locale = targetLocale |
|
|
|
configuration.locale = targetLocale |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
resources.updateConfiguration(configuration, resources.displayMetrics) |
|
|
|
resources.updateConfiguration(configuration, resources.displayMetrics) |
|
|
|
context |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 当前系统语言 |
|
|
|
* 当前系统语言 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|