How to get language (locale) currently Android app displays?

后端 未结 5 2022
遥遥无期
遥遥无期 2020-12-09 02:06

How to get know language (locale) currently Android app uses to display texts to user?

I know I can use Locale.getDefault() to get default OS locale. Bu

5条回答
  •  时光说笑
    2020-12-09 02:38

    You can use the code below. For example, the functions presented below may be placed inside the class extended the Application class.

    public class MyApplication extends Application {
        ...
        public static String APP_LANG;
        private Context ctx = getBaseContext();
        private Resources res = ctx.getResources();
        public SharedPreferences settingPrefs;
        ...
    
        public void restoreAppLanguage(){
        /**
        *Use this method to store the app language with other preferences.
        *This makes it possible to use the language set before, at any time, whenever 
        *the app will started.
        */
            settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
            String lang = settingPrefs.getString("AppLanguage", "");
            if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
                Locale myLocale;
                myLocale = new Locale(lang);
                Locale.setDefault(myLocale);
                Configuration config = new Configuration();
                config.locale = myLocale;
                res.updateConfiguration( config, res.getDisplayMetrics() );
            }
        }
    
        public void storeAppLanguage(int lang) {
        /**
        *Store app language on demand
        */
            settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
            Editor ed = settingPrefs.edit();
    
            Locale myLocale;
            myLocale = new Locale(lang);
            Locale.setDefault(myLocale);
            Configuration config = new Configuration();
            config.locale = myLocale;
            res.updateConfiguration( config, res.getDisplayMetrics() );
            ed.putString("AppLanguage", lang);
    
            ed.commit();
        }
    
        public void setAppLanguage(String lang){
        /**
        *Use this method together with getAppLanguage() to set and then restore
        *language, whereever you need, for example, the specifically localized
        *resources.
        */
            Locale myLocale;
            myLocale = new Locale(lang);
            Locale.setDefault(myLocale);
            Configuration config = new Configuration();
            config.locale = myLocale;
            res.updateConfiguration( config, res.getDisplayMetrics() );
        }
    
        public void getAppLanguage(){
        /**
        *Use this method to obtain the current app language name
        */
            settingPrefs = getSharedPreferences("ConfigData",MODE_PRIVATE);
            String lang = settingPrefs.getString("AppLanguage", "");
            if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
                APP_LANG = lang;
            }
            else APP_LANG = Locale.getDefault().getLanguage();
        }
    }
    

    And then wherever in the main code we can write:

    public class MainActivity extends ActionBarActivity {
        ...
        MyApplication app;
        ... 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            app = (MyApplication)getApplicationContext();
            ...
            if(settingPrefs.getAll().isEmpty()){
                //Create language preference if it is not
                app.storeAppLanguage(Locale.getDefault().getLanguage());
            }
    
            //Restore previously used language
            app.restoreAppLanguage();
            ...
        }
        ...
        void SomethingToDo(){
            String lang = "en"; //"fr", "de", "es", "it", "ru" etc.
            ...
            app.getAppLanguage();
            app.setAppLanguage(lang);
            ...
            //do anything
            ...
            app.setAppLanguage(app.APP_LANG);
        }
        ...
    }
    

    In your case, you, shortly, may use getAppLanguage() and then check the public variable APP_LANG to obtain what language is currently used.

提交回复
热议问题