版权声明:本文为博主原创文章,未经博主允许不得转载。
最近项目上产品经理提了个需求,要求关闭语言国际化,不管手机系统设置那个国家的语言,都要显示汉语,好吧,既然有需求,那就做吧。但是项目中已经有英文的配置了,且是作为默认String提供的,这么多翻译好的文字,直接删除掉替换成中文为默认String又感觉弃之可惜。故网上Google下解决方案。就开始往下看吧。
一、代码中动态设置应用显示语言(手动控制使用values-zh-rCN下字符串)
public static void initAppLanguage(Context context) { if (context == null) { return; } Locale.setDefault(Locale.CHINA); Configuration config = context.getResources().getConfiguration(); config.locale = Locale.CHINA; context.getResources().updateConfiguration(config , context.getResources().getDisplayMetrics()); }
二、语言切换监听
1.广播方式监听
/** * ClassName: LocaleChangeReceiver * Description:(这里用一句话描述这个类的作用) * Created by chensf on 2016-8-17 16:47. */ public class LocaleChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v(TAG, "mReceiver onReceive intent.getAction(): "+intent.getAction()); if(intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED)) { Log.e("LocaleChangeReceiver","Language change"); SystemUtil.initAppLanguage(context); } } }
2.重写onConfigurationChanged方法
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<activity android:name=".MainActivity" android:configChanges="locale|layoutDirection"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
@Override public void onConfigurationChanged(Configuration newConfig) { Log.e("MyApplication","onConfigurationChanged"); super.onConfigurationChanged(newConfig); SystemUtil.initAppLanguage(getBaseContext()); }这个配置是整个应用设置的,在这里设置语言,能够改变整个应用的语言设置,而且这种方式,无需在AndroidManifest中增加配置,我测试过,在Application中重写onConfigurationChanged,Activity的onConfigurationChanged
3万+



被折叠的 条评论
为什么被折叠?



