android - Android 应用程序区域设置语言更改不适用于 EN,但适用于其他语言
问题描述
我正在尝试将FR
和添加EN
到我的应用程序中,因此我使用编辑器来翻译字符串,从而创建 astrings.xml(en-rGB)
和 a strings.xml(fr-fFR)
。我在微调器中使用它们,所以当我选择RO
(默认语言)或FR
整个应用程序中的字符串时会发生变化。但是当我选择EN
它时,它不会改变任何字符串。我同时创建了这两个FR
,EN
所以我似乎找不到它们之间的任何区别,这可能会使应用程序不像我喜欢的那样运行。
我在语言之间切换的代码:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getCountryName();
pozitie = position;
if(!clickedCountryName.equals(lastCountryName)) {
lastCountryName = clickedCountryName;
if (clickedCountryName.equals("FR")){
setLocale("fr");
recreate();
} else if (clickedCountryName.equals("EN")){
setLocale("en");
recreate();
}else if(clickedCountryName.equals("RO")){
setLocale("ro");
recreate();
}
}
}
并且创建的列表,我仔细检查了,所以不会有任何错字:
private void initList() {
mCountryList = new ArrayList<>();
mCountryList.add(new TaraITEM("RO", R.drawable.steag_ro));
mCountryList.add(new TaraITEM("EN", R.drawable.steag_en));
mCountryList.add(new TaraITEM("FR", R.drawable.steag_fr));
}
编辑:添加setLocale
方法:
private void setLocale(String limba){
Locale locale = new Locale(limba);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale= locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = getSharedPreferences("Setare limba", MODE_PRIVATE).edit();
editor.putString("Limba mea", limba);
editor.putInt("Pozitie", pozitie);
editor.apply();
}
解决方案
我已经设法通过添加另一个locale
for来解决这个问题,RO
因为Android Studio
hasen
作为默认语言。所以源strings.xml
文件将用作en
locale
. 在我的情况下,默认strings.xml
有ro
翻译,所以当我尝试使用en
时,应用程序会使用default
, 所以罗马尼亚语翻译。
推荐阅读
- database - Oracle 表空间计算
- javascript - 为 jquery 动态生成的文本保留空间
- javascript - React 路由器和浏览器历史记录
- woocommerce - 隐藏产品查询未返回结果的产品类别
- typescript - (参数)错误:ValidationError Type 'undefined' 不能用作索引类型
- sql-server - 使用 OUTER APPLY 导入 JSON 文件并发现键名
- image - 为什么我的网站上的图片在手机上与桌面上的图片不同?
- python - webelement的相同xpath,但有时不起作用 - 抓取亚马逊
- asynchronous - NuxtJS 存储返回未定义的本地存储值
- python - 如何使用 json 库在 Python 中解码这种类型的 json?