belyxiong的专栏

关于Android

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

首先创建两个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);



阅读更多
上一篇Android 5.0(L) 的MediaSession架构分析
下一篇获取Andorid的ListView在完成刷新后的事件
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭