Change app language programmatically in Android

前端 未结 30 3066
面向向阳花
面向向阳花 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条回答
  •  -上瘾入骨i
    2020-11-21 05:09

    Just adding an extra piece that tripped me up.

    While the other answers work fine with "de" for example

    String lang = "de";
    Locale locale = new Locale(lang); 
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, 
        getBaseContext().getResources().getDisplayMetrics());
    

    The above wont work with for example "fr_BE" locale so it would use the values-fr-rBE folder or similar.

    Needs the following slight change to work with "fr_BE"

    String lang = "fr";
    
    //create a string for country
    String country = "BE";
    //use constructor with country
    Locale locale = new Locale(lang, country);
    
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, 
        getBaseContext().getResources().getDisplayMetrics());
    

提交回复
热议问题