首页 > 解决方案 > Android 应用程序区域设置语言更改不适用于 EN,但适用于其他语言

问题描述

我正在尝试将FR和添加EN到我的应用程序中,因此我使用编辑器来翻译字符串,从而创建 astrings.xml(en-rGB)和 a strings.xml(fr-fFR)。我在微调器中使用它们,所以当我选择RO(默认语言)或FR整个应用程序中的字符串时会发生变化。但是当我选择EN它时,它不会改变任何字符串。我同时创建了这两个FREN所以我似乎找不到它们之间的任何区别,这可能会使应用程序不像我喜欢的那样运行。

在此处输入图像描述

我在语言之间切换的代码:

 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();
}

标签: android

解决方案


我已经设法通过添加另一个localefor来解决这个问题,RO因为Android Studiohasen作为默认语言。所以源strings.xml文件将用作en locale. 在我的情况下,默认strings.xmlro翻译,所以当我尝试使用en时,应用程序会使用default, 所以罗马尼亚语翻译。


推荐阅读