App 国际化语言

流程:
1、准备国际化资源文件。
2、应用内切换语言
3、解决语言切换相关问题

切换语言关键代码

public static void updateAppLanguage(Context context){
        String language = "en";
        Locale myLocale = new Locale(language);
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration config = res.getConfiguration();
        config.locale = myLocale;
        res.updateConfiguration(config, dm);
    }

集中问题
1、切换后app语言随系统变化
2、activity横屏时跟随系统语言

解决方案
1、监听广播

@Override
public void onReceive(Context context, Intent intent) {
    if(Objects.equals(intent.getAction(), Intent.ACTION_LOCALE_CHANGED)) {
        AppLanguageUtil.updateAppLanguage(context);
    }
}

2、自定义MyApp 继承Application,重写 onConfigurationChanged 函数

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    AppLanguageUtil.updateAppLanguage(this);
}

newConfig此时传入的是系统config,所以需要添加自己的业务。

你可能感兴趣的:(安卓专栏,app,国际化)