首页 文章

Android更改中文和繁体中文的区域设置无法正常工作

提问于
浏览
2

在我的应用程序中,我可以选择从中文切换到繁体中文 .

我使用的是微调器,其中1号是中文,2号是繁体中文 . 选择位置1时,这是我的代码切换语言

if (pos == 0) 
{
   langSelected ="en";
}               
else if (pos == 1) 
{
   langSelected ="zh";
}               
else if (pos == 2)
{
  langSelected ="zh-rTW";
}
Locale locale = new Locale(lang);           
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
    if (locale != null){
        newConfig.locale = locale;
        Locale.setDefault(locale);
        getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
     }
}

使用微调器从英语切换到中文时加载了正确的语言,但加载繁体中文(zh-rTW)时,只加载中文文本

我在values-zh中有简体中文文本,因为我在values-zh-rTW中加载了繁体中文文本

应用程序名称因每种语言而异,所以我也尝试从设备设置更改语言,现在也在简体中文中正确加载,但繁体中文没有加载 . 但是这里的繁体中文的应用程序名称已更改,即应用程序名称从值-zh-rTW加载

哪里出错了,我是否应该更改繁体中文的文件夹?

3 回答

  • -1

    此代码更改了简体中文和繁体中文的语言环境:

    public void setAppLocale(Context context, String languageCode, String countryCode){
        Resources resources = context.getResources();
        Locale locale = new Locale(languageCode, countryCode);
        Locale.setDefault(locale);
        Configuration configuration = new Configuration();
        configuration.setLocale(locale);
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }
    
  • 0

    我知道这是一个迟到的帖子,但希望它有助于某人......

    解决方案是使用国家/地区名称简单地创建区域设置 . 这意味着 Locale 类已经声明了一些静态语言环境 . 例如:-

    中国区域 - https://developer.android.com/reference/java/util/Locale.html#CHINA

    台湾当地 - https://developer.android.com/reference/java/util/Locale.html#TAIWAN

    简而言之,解决方案是: -

    Locale locale;
    if(lang.equals("zh-rTW"))
        locale = Locale.TAIWAN;
    else(lang.equals("zh-rCN")
        locale = Locale.CHINA;
    else
        //handle other languages
    
  • 2

    根据我的经验,最好是简体中文 values-zh-rCN 和传统的 values-zh-rTW . 此外,这可能已经是您在代码中进一步做的事情,手动更改区域设置需要重新加载活动才能生效 . 所以一个简单的 Finish()StartActivity() 就足够了 .

相关问题