您的位置:首页 > 移动开发 > Android开发

Android中获取不同语言目录下的字符串

2015-07-09 17:28 351 查看
首先创建两个API:
private final static String LAN_FR = "fr";
private final static String LAN_ES = "es";


Resources getResourcesByLocale( Resources res, String localeName ) { Configuration conf = new Configuration(res.getConfiguration()); conf.locale = new Locale(localeName); return new Resources(res.getAssets(), res.getDisplayMetrics(), conf); } private void resetLocale(Resources res){ Configuration conf = new Configuration(res.getConfiguration()); conf.locale = mCurLocale; new Resources(res.getAssets(), res.getDisplayMetrics(), conf); }

然后使用:

Resources res = mContext.getResources();
if(res != null){
mCurLocale = res.getConfiguration().locale;  //得到当前的语言

Resources res_es = getResourcesByLocale(res, LAN_ES); //得到指定语言的资源
//取对应语言的字符串资源的值
mEqTypeStr_ES[EqualizerType.Custom.ordinal()] = res_es.getString(R.string.audio_custom);
mEqTypeStr_ES[EqualizerType.Talk.ordinal()] = res_es.getString(R.string.audio_talk);
mEqTypeStr_ES[EqualizerType.Classical.ordinal()] = res_es.getString(R.string.audio_classical);

//重置当前资源为当前语言的资源,必须要这一步
resetLocale(res);

//再取其他语言的对应资源
Resources res_fr = getResourcesByLocale(res, LAN_FR);
mEqTypeStr_FR[EqualizerType.Custom.ordinal()] = res_fr.getString(R.string.audio_custom);
mEqTypeStr_FR[EqualizerType.Talk.ordinal()] = res_fr.getString(R.string.audio_talk);
mEqTypeStr_FR[EqualizerType.Classical.ordinal()] = res_fr.getString(R.string.audio_classical);
mEqTypeStr_FR[EqualizerType.Rock.ordinal()] = res_fr.getString(R.string.audio_rock);
mEqTypeStr_FR[EqualizerType.Jazz.ordinal()] = res_fr.getString(R.string.audio_jazz);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: