Change app language programmatically in Android

前端 未结 30 2994
面向向阳花
面向向阳花 2020-11-21 04:34

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific lan

30条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 05:16

    This code really works:

    fa = Persian, en = English

    Enter your language code in languageToLoad variable:

    import android.app.Activity;
    import android.content.res.Configuration;
    import android.os.Bundle;
    
    public class Main extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        String languageToLoad  = "fa"; // your language
        Locale locale = new Locale(languageToLoad); 
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, 
          getBaseContext().getResources().getDisplayMetrics());
        this.setContentView(R.layout.main);
      }
    }
    

提交回复
热议问题