Android how to change the application language at runtime

混江龙づ霸主 提交于 2019-12-29 03:35:08

问题


I want to let the user change the language of my application using spinner (or any way). I tried many ways but they change the language of this activity not all activities, and I want to save it so when the user restart the app he will find the last choosed language.


回答1:


you can use this code in spinner or any way you want

String languageToLoad  = "en"; // your language 
Locale locale = new Locale(languageToLoad);  
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config,  
getBaseContext().getResources().getDisplayMetrics());

then you should save the language like this

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();  

and use the same code in every activity in onCreate() to load the languageToLoad from the SharedPreferences




回答2:


Use SharedPreferences to keep track of the language the user chose, and then set the activities to use that language in the onCreate (), and maybe onResume() method. This way it will persist across app restarts etc.




回答3:


This is an old question, but I'll answer it anyway :-) You can extend Application class to apply Abol3z's solution on every Activity. Create class:

public class MyApplication extends Application {

   @Override
   public void onCreate() {
       super.onCreate();
       SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
       String lang = preferences.getString("lang", "en");
       Locale locale = new Locale(lang);
       Locale.setDefault(locale);
       Configuration config = new Configuration();
       config.locale = locale;
       getBaseContext().getResources().updateConfiguration(config,
               getBaseContext().getResources().getDisplayMetrics());    
   }  
}

And set MyApplication as application class in manifest:

 <application
        android:name=".MyApplication"
        ...
 />

You can set the lang value (in your spinner):

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
preferences.edit().putString("lang", "en").commit();



回答4:


btnChange.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
            //preferences.edit().putString("lang", "bn").commit();

            String lang = preferences.getString("lang", "en");
            //Log.e("lang", "lang in Main Activity:"+lang);
            if (lang.equalsIgnoreCase("en")){
                setLocale("bn");
                preferences.edit().putString("lang", "bn").commit();
                btnChange.setText("Eng");
            }else if(lang.equalsIgnoreCase("bn")){
                setLocale("en");
                preferences.edit().putString("lang", "en").commit();
                btnChange.setText("বাংলা");
            }
        }
    });


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(this, MainActivity.class);
    startActivity(refresh);
    finish();
}

we use two language for test purpose. keep all string in different folder named values and values-bn.



来源:https://stackoverflow.com/questions/12230553/android-how-to-change-the-application-language-at-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!