以编程方式更改应用程序的语言,而无需刷新整个应用程序 [英] Change Language of the application programmatically without refreshing the whole app

查看:94
本文介绍了以编程方式更改应用程序的语言,而无需刷新整个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户的输入更改应用程序的语言.我尝试使用此代码来更改应用程序的语言,并且运行良好.

I'm trying to change the language of the application according to the user's input. I tried using this code to change the language of the application and it's working pretty fine.

public void setLocale(String lang) {
    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(MainActivity.this, MainActivity.class);
    startActivity(refresh);
}

但是问题是应用程序必须重新启动/刷新才能重新加载资源.

But the problem is that app has to restart/refresh in order to reload the resources.

  • 这是通过程序设置应用程序语言的正确方法吗?
  • 是否有其他方法可以在不刷新应用的情况下更改语言?

推荐答案

尝试对您的活动使用recreate().在我看来,这种方法是成功的.如果您使用的是Fragment,请使用getActivity().recreate();

Try with recreate() on your activity. This approach was successful in my case. If you are on Fragment, then use getActivity().recreate();

SharedPreferences.Editor editor = prefs.edit();
                editor.putString(Constants.APP_STATE.SAVED_LOCALE, localeString);
                editor.apply();
                getActivity().recreate();

覆盖您的活动的以下方法:

Override following method of your activity:

@Override
protected void attachBaseContext(Context newBase) {
    SharedPreferences prefs = newBase.getSharedPreferences(Constants.APP_STATE.STATE_SHARED_PREFERENCES, MODE_PRIVATE);
    String localeString = prefs.getString(Constants.APP_STATE.SAVED_LOCALE, Constants.DEFAULTS.DEFAULT_LOCALE);
    Locale myLocale = new Locale(localeString);
    Locale.setDefault(myLocale);
    Configuration config = newBase.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(myLocale);
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
            Context newContext = newBase.createConfigurationContext(config);
            super.attachBaseContext(newContext);
            return;
        }
    } else {
        config.locale = myLocale;
    }
    super.attachBaseContext(newBase);
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());

当用户设置了所需的语言环境时,您要做的是将其保存到SharedPreferences中的某个字符串中,并调用activity的recreate().然后,这将调用attachBaseContext(Context context),并且在此方法中,将正确的语言环境设置为配置,然后将使用此配置创建新的上下文.之后,新的上下文将被发送到超类,该超类将更新应用程序上下文并显示正确的语言环境.

When user set wanted locale, what you want to do is to save it into some string in SharedPreferences and call recreate() of activity. This will then call attachBaseContext(Context context) and in this method proper locale will be set to configuration, then new context will be created with this configuration. After that, new context will be sent to super class which will update application context and proper locale will be shown.

这也很好,因为下次启动应用程序时会自动设置区域设置.

It is also good because locale is automatically set when starting app next time.

这篇关于以编程方式更改应用程序的语言,而无需刷新整个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆