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

时间:2015-02-11 08:11:20

标签: android string localization

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

我正在使用微调器,其中位置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中有简体中文文本,因为我已将值为-zh-rTW的繁体中文文本加载

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

加载

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

3 个答案:

答案 0 :(得分:4)

此代码更改了简体中文和繁体中文的区域设置:

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

答案 1 :(得分:3)

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

解决方案是使用国家/地区名称创建区域设置。这意味着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 :(得分:0)

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