在Kotlin中以编程方式更改区域设置 [英] Change locale programmatically in Kotlin
问题描述
我有一些代码可以在Java中以编程方式更改语言环境.但是,当我的应用程序迁移到Kotlin时,我无法再更改语言环境.
I had some codes for change locale programmatically in Java. But when my application migrated to Kotlin, I can't change locale any more.
例如,此Java代码非常有效:
For example this code in Java worked very good :
public static final void setAppLocale(String language, Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Resources resources = activity.getResources();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(new Locale(language));
activity.getApplicationContext().createConfigurationContext(configuration);
} else {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = activity.getResources().getConfiguration();
config.locale = locale;
activity.getResources().updateConfiguration(config,
activity.getResources().getDisplayMetrics());
}
}
我在Kotlin中尝试了许多代码,但没有一个对我有用.这是我的最后尝试:
I tried many codes in Kotlin but non of them worked for me. This is my last try:
fun changeLanguage(context: Context, language : String) {
val locale = Locale(language)
Locale.setDefault(locale)
val config = context.resources.configuration
config.setLocale(locale)
context.createConfigurationContext(config)
context.resources.updateConfiguration(config, context.resources.displayMetrics)
}
如何更改Kotlin中的应用程序本地?用Java编写的旧代码在Kotlin应用程序中不起作用.
How can I change application's local in Kotlin? Old codes that were written in Java did not work in Kotlin application.
推荐答案
创建一个上下文帮助器类,比如
Create a Context helper class let's say
class ApplicationLanguageHelper(base: Context) : ContextThemeWrapper(base, R.style.AppTheme) {
companion object {
fun wrap(context: Context, language: String): ContextThemeWrapper {
var context = context
val config = context.resources.configuration
if (language != "") {
val locale = Locale(language)
Locale.setDefault(locale)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config, locale)
} else {
setSystemLocaleLegacy(config, locale)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(locale)
context = context.createConfigurationContext(config)
} else {
context.resources.updateConfiguration(config, context.resources.displayMetrics)
}
}
return ApplicationLanguageHelper(context)
}
@SuppressWarnings("deprecation")
fun setSystemLocaleLegacy(config: Configuration, locale: Locale) {
config.locale = locale
}
@TargetApi(Build.VERSION_CODES.N)
fun setSystemLocale(config: Configuration, locale: Locale) {
config.setLocale(locale)
}
}
}
在活动"中,您可以覆盖attachBaseContext
And in your Activity you can override attachBaseContext
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(ApplicationLanguageHelper.wrap(newBase!!, "fa"))
}
要更改语言,可以使用微调器或其他任何首选方式,将以下方法称为OnClick
To Change the language you can use a Spinner or any other preferred way, to call the following method OnClick
private fun changeApplicationLanguage(language:String){
val sharedPreferencesEditor = sharedPreferences.edit()
when (language) {
ENGLISH -> sharedPreferencesEditor?.putString(SELECTED_LANGUAGE, ENGLISH)
PERSIAN -> sharedPreferencesEditor?.putString(SELECTED_LANGUAGE, PERSIAN)
PASHTO -> sharedPreferencesEditor?.putString(SELECTED_LANGUAGE, PASHTO)
}
sharedPreferencesEditor.putBoolean(LANGUAGE_IS_SELECTED, true)
sharedPreferencesEditor?.apply()
recreate()
}
确保您定义sharedPreferences
以及SELECTED_LANGUAGE
,ENGLISH
等常量
make sure you define sharedPreferences
and also SELECTED_LANGUAGE
, ENGLISH
etc, consts
const val SELECTED_LANGUAGE = "language"
const val ENGLISH = "en"
const val PERSIAN = "fa"
const val PASHTO = "ps"
private lateinit var sharedPreferences: SharedPreferences
,并在setContent
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this@Your_Activity_Name)
还通过添加以下代码来覆盖基本上下文
also Override the base context by adding the following code
// Overwrite the context
override fun attachBaseContext(newBase: Context?) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(newBase)
val lang = sharedPreferences.getString(SELECTED_LANGUAGE, "en")
super.attachBaseContext(ApplicationLanguageHelper.wrap(newBase!!, lang!!))
}
我确信此方法不是最佳解决方案,但这可能会有帮助
I am sure this method is not an optimal solution, but, this might help
这篇关于在Kotlin中以编程方式更改区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!