Save language chosen by user, Android

前端 未结 4 1891
醉话见心
醉话见心 2021-01-14 11:58

How can I change default string.xml? I can change my app to five different languages, but whenever I close the app and reopen it. It goes back to default English.

Is

4条回答
  •  生来不讨喜
    2021-01-14 12:28

    Here is some code that works for me:

    public class  MainActivity extends AppCompatActivity {
    public static String storeLang;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this);
        storeLang = shp.getString(getString(R.string.key_lang), "");
    
        // Create a new Locale object
        Locale locale = new Locale(storeLang);
    
        // Create a new configuration object
        Configuration config = new Configuration();
        // Set the locale of the new configuration
        config.locale = locale;
        // Update the configuration of the Accplication context
        getResources().updateConfiguration(
                config,
                getResources().getDisplayMetrics()
        );
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

    }

    Source: https://blog.lokalise.co/android-app-localization/

提交回复
热议问题