我在Java中以编程方式有一些代码用于更改区域设置。但是当我的应用程序迁移到Kotlin时,我无法再更改语言环境。
例如,Java中的这段代码非常好用:
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尝试过很多代码但是没有代码在我身上工作。这是我的最后一次尝试:
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应用程序中不起作用。
答案 0 :(得分:1)
试试这个
fun setAppLocale(languageFromPreference: String?, context: Context)
{
if (languageFromPreference != null) {
val resources: Resources = context.resources
val dm: DisplayMetrics = resources.displayMetrics
val config: Configuration = resources.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(Locale(languageFromPreference.toLowerCase(Locale.ROOT)))
} else {
config.setLocale(Locale(languageFromPreference.toLowerCase(Locale.ROOT)))
}
resources.updateConfiguration(config, dm)
}
}
您可以通过从活动中访问此功能来设置活动中的应用程序区域
..
super.onCreate(savedInstanceState)
setAppLocale(pref.getLanguageFromPreference().toString(), this)
setContentView(R.layout.activity_main)
...
pref.getLanguageFromPreference() 返回语言字符串(例如:“en”)
答案 1 :(得分:0)
创建一个上下文帮助器类,比如
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
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(ApplicationLanguageHelper.wrap(newBase!!, "fa"))
}