Changing locale: Force activity to reload resources?

后端 未结 6 1472
感情败类
感情败类 2020-11-30 06:24

So I have a language setting in my application. When the language is switched, I would like all the textviews etc to change language immediately. Currently I just change the

相关标签:
6条回答
  • 2020-11-30 06:55

    Assuming you're changing the language through something like

    private void updateLocale(@NonNull final Context context,
                              @NonNull final Locale newLocale) {
        final Resources resources = context.getResources();
        final DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        final Configuration configuration = resources.getConfiguration();
        configuration.locale = newLocale;
        resources.updateConfiguration(configuration, displayMetrics);
        Locale.setDefault(newLocale);
    }
    

    You'll need to call Activity.recreate() in all currently open activities, which is what would happen if the user changed the system language while you were not subscribing to android:configChanges="locale".

    0 讨论(0)
  • 2020-11-30 07:03

    I think the question is switching language in runtime for the app and displaying localized messages in UI. android:configChanges="locale" calls onConfigurationChanged if the system locale is changed (in setting of your device) while the app is running, and not if you change locale in the code for your app, which I guess is what you want to accomplish. That's why it's not refreshing.

    0 讨论(0)
  • 2020-11-30 07:07

    Here is the method I use during every activity onCreate() or onResume() depending on my needs (if my activity will be resuming after user changed language settings or will always be created with language already set):

    From there I just refresh the view manually or from onConfigurationChanged() which get called after this method finishes.

    public static void changeLocale(Activity activity, String language)
    {
        final Resources res = activity.getResources();
        final Configuration conf = res.getConfiguration();
        if (language == null || language.length() == 0)
        {
            conf.locale = Locale.getDefault();
        }
        else
        {
            final int idx = language.indexOf('-');
            if (idx != -1)
            {
                final String[] split = language.split("-");
                conf.locale = new Locale(split[0], split[1].substring(1));
            }
            else
            {
                conf.locale = new Locale(language);
            }
        }
    
        res.updateConfiguration(conf, null);
    }
    
    0 讨论(0)
  • 2020-11-30 07:07
    public void settingLocale(Context context, String language) {
    
    Locale locale;
    
    Configuration config = new Configuration();
    
     if(language.equals(LANGUAGE_ENGLISH)) {
    
        locale = new Locale("en");
    
        Locale.setDefault(locale);
    
        config.locale = locale;
    
    }else if(language.equals(LANGUAGE_ARABIC)){
    
        locale = new Locale("hi");
    
        Locale.setDefault(locale);
    
        config.locale = locale;
    
    }
    
    context.getResources().updateConfiguration(config, null);
    
    // Here again set the text on view to reflect locale change
    
    // and it will pick resource from new locale
    
    tv1.setText(R.string.one); //tv1 is textview in my activity
    
    }
    
    0 讨论(0)
  • 2020-11-30 07:15

    I'm not sure why this isn't picked up by onConfigurationChanged().

    Hey, sandis, do you mean the method onConfigurationChanged() doesn't called in your activity when you changed the language? I met the same problem. The problem maybe this: when we change the language, the activity goes to onDestroy()(you can try this), so there is nobody to call onConfigurationChanged(). When we launch the activity again, the onCreate() is called, not the onConfigurationChanged(). There maybe something different in locale change and orientation change.

    0 讨论(0)
  • 2020-11-30 07:21

    In your AndroidManifest.xml, add this attribute to your Activity

    android:configChanges="locale"
    

    In your activity override onConfigurationChanged()

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      // refresh your views here
      super.onConfigurationChanged(newConfig);
    }
    

    https://developer.android.com/guide/topics/manifest/activity-element.html#config

    0 讨论(0)
提交回复
热议问题