关闭

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

2985人阅读 评论(0) 收藏 举报
首先创建两个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);



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:111503次
    • 积分:1587
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:8篇
    • 译文:0篇
    • 评论:22条
    最新评论