After the screen rotation, the language of my application will be changed

后端 未结 3 1528
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 17:58

I have created a bilingual (having two languages) android application. I have inserted my resource strings in two files:

For Persian language (default)
values/st         


        
相关标签:
3条回答
  • 2021-02-13 18:29

    You can make class which extends Application. There you can override one method which gets called everytime you change configuration (example when screen orientation gets changed).

    Something like:

    public class MyApplication extends Application {
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            setLocale();
        }
    
        private void setLocale() {
            Locale locale = new Locale("fa_IR");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                  getBaseContext().getResources().getDisplayMetrics());
        }
    }
    

    And dont forget to declare it in the manifest: example of Application class

    In AndroidManifest.xml:

    <application
        android:name="com.blaablaa.bumbam.MyApplication"
        ...
    
    0 讨论(0)
  • 2021-02-13 18:30

    I don't exactly understand what you are trying to do. But in general you don't have to manage the language of the app. Android will automatically pick the phone language if available and in any other case bring up a dialog to pick the language.

    If you do want to set language independent from that with your code - which I would not recommend - then there is probably a problem with your lifecycle preventing your code from being run again after an orientation change. I can help you with that if you post more of your code.

    0 讨论(0)
  • 2021-02-13 18:39

    You can create custom ContextWrapper

    public class mContextWrapper extends ContextWrapper {
        public mContextWrapper(Context base){
            super(base);
        }
    
        @SuppressWarnings("deprecation")
        public static ContextWrapper wrap(Context context, String language) {
            Configuration config = context.getResources().getConfiguration();
            Locale sysLocale = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                sysLocale = getSystemLocale(config);
            } else {
                sysLocale = getSystemLocaleLegacy(config);
            }
            if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
                Locale locale = new Locale(language);
                Locale.setDefault(locale);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    setSystemLocale(config, locale);
                } else {
                    setSystemLocaleLegacy(config, locale);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    context = context.createConfigurationContext(config);
                } else {
                    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
                }
            }
            return new mContextWrapper(context);
        }
    
        @SuppressWarnings("deprecation")
        public static Locale getSystemLocaleLegacy(Configuration config){
            return config.locale;
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        public static Locale getSystemLocale(Configuration config){
            return config.getLocales().get(0);
        }
    
        @SuppressWarnings("deprecation")
        public static void setSystemLocaleLegacy(Configuration config, Locale locale){
            config.locale = locale;
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        public static void setSystemLocale(Configuration config, Locale locale){
            config.setLocale(locale);
        }
    }
    

    and in your activity attachBaseContext attach your custom ContextWrapper

     @Override
        protected void attachBaseContext(Context newBase) {
            globalClass = (YouAppClass)newBase.getApplicationContext();
            //try geting the lang you have setted inside your YouAppClass class
            // or you can use shared preferences for it 
            //pref = PreferenceManager.getDefaultSharedPreferences(newBase)
            lang=globalClass .getLang();
            super.attachBaseContext(mContextWrapper.wrap(newBase,lang));
        }
    
    0 讨论(0)
提交回复
热议问题