ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-05-28 14:12:08  阅读:171  来源: 互联网

标签:android localization string


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

我使用的是微调器,其中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加载

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

解决方法:

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

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

标签:android,localization,string
来源: https://codeday.me/bug/20190528/1171465.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有