【发布时间】:2018-04-02 00:44:20
【问题描述】:
我正在尝试使用以下解决方案以编程方式更改 android 上的语言环境:
mConfiguration = new Configuration(getApplicationContext().getResources().getConfiguration());
mConfiguration.setLocale(new Locale("es"));
然后当我想要一个字符串资源时使用这个配置,如下所示:
public static String getString(int id)
{
return getApplicationContext().createConfigurationContext(mConfiguration).getResources().getString(id);
}
对于我想要使用的每种语言,我确实有单独的文件夹,并且每当我调用上述 getString(R.string.thisPerson) 函数时,我都会获取特定于语言的字符串。我在所有特定于语言环境的 strings.xml 文件中都有 thisPerson 字符串。
但是,每当我直接在布局文件中引用字符串资源时,如下所示:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="@string/thisPerson"
android:layout_marginRight="10dp"
/>
上述字符串资源不尊重语言环境。我总是得到默认字符串(来自默认的strings.xml 文件),而不是来自当前语言环境的字符串。我可能会遗漏什么,所以我没有在视图布局中获取特定于语言环境的字符串,而是在我调用 getString() 函数时得到它?
【问题讨论】: