首先创建两个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中获取不同语言目录下的字符串
最新推荐文章于 2022-03-19 21:55:09 发布