【发布时间】:2019-07-24 23:16:03
【问题描述】:
我的应用支持 3 种语言(英语、德语、俄语)。我使用 LocaleHelper 类进行语言设置并设置语言如下:
lateinit var alert: AlertDialog
val options = arrayOf(
resources.getString(R.string.en_lang),
resources.getString(R.string.de_lang),
resources.getString(R.string.ru_lang))
val dialogBuilder = AlertDialog.Builder(this, R.style.AboutAlertDialogStyle)
dialogBuilder.setTitle(resources.getString(R.string.app_language))
.setSingleChoiceItems(options, position) { _, which ->
when {
options[which] == resources.getString(R.string.en_lang) -> {
LocaleHelper.setLocale(baseContext, "en").resources
alert.dismiss()
recreate()
}
options[which] == resources.getString(R.string.de_lang) -> {
LocaleHelper.setLocale(baseContext, "de").resources
alert.dismiss()
recreate()
}
else -> {
LocaleHelper.setLocale(baseContext, "ru").resources
alert.dismiss()
recreate()
}
}
}
.setNegativeButton(resources.getString(R.string.cancel)) { dialog, _ ->
dialog.cancel()
}
alert = dialogBuilder.create()
alert.show()
英语和德语一切正常。但不适合俄罗斯人。 我还根据post这个post的第一个答案更改了LocaleHelper类中Locale的输入,如下:
public static Context setLocale(Context context, String language) {
persist(context, language);
Configuration configuration;
Resources resources;
Locale locale = null;
if (language.equals("ru")) {
locale = new Locale(language, "RU");
Locale.setDefault(locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
locale = new Locale(language);
Locale.setDefault(locale);
resources = context.getResources();
configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
我怎样才能让它运行俄语。当用户选择它时,应用程序会将语言改为英语。
非常感谢!
【问题讨论】:
标签: android localization locale