【发布时间】:2015-03-05 20:15:57
【问题描述】:
我尝试在一个 android 应用程序中更改我的语言环境。 如果我只使用语言一切都可以,但今天我在我的应用程序中添加了葡萄牙语-br 翻译。
代码:
Locale locale;
if(language.contains("-")) // In this case, the locale specify also the country
{
String[] country_locale = language.split("-");
locale = new Locale(country_locale[0], country_locale[1]);
}
else
locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
在最后一行之前一切正常。 我知道,因为在我的程序的前一部分中,我可以使用以下代码获得一些具有正确 pt-br 语言环境的字符串:
Resources resources = new Resources(ctx.getAssets(), ctx.getResources().getDisplayMetrics(), new_config);
如果使用国家/地区代码定义了区域设置,则 updateConfiguration 将我的区域设置设置为英语。
String.xml 在 values-pt-rBR 中
在调试时区域设置值设置为 pt-BR。
编辑:经过进一步测试,这适用于我的 Android 手机,但不适用于我的平板电脑(Sambung 和 android 4.4.2)。 可能是什么原因?
编辑 2:如果我使用手机,也可以通过模拟器工作,如果我使用平板电脑,则无法工作。
【问题讨论】:
标签: android localization locale