Locale not set Programatically in Android 5.0 Lollipop

强颜欢笑 提交于 2019-12-07 12:16:12

问题


My application set locale according to selected language in application. Up to Kitkat my code works fine. After update to Lollipop locale was not set. Here i paste my code to set locale..

Locale locale = new Locale("de_DE");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);

回答1:


You have to change the way of the locale initialization. From this:

Locale locale = new Locale("de_DE");

to this:

String language = "de";
String country = "DE";
Locale locale = new Locale(language , country);

Check out the full response here https://stackoverflow.com/a/27490553/2659558

Cheers!




回答2:


you can set locale to application by using following code.


Locale locale = new Locale("de", "DE");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);



回答3:


Check out Lollipop Set Default local does not work Try using just the language code "de" rather than "de_DE".




回答4:


you can use this code:

 public static final String COUNTRY_EN = "GB";
  public static final String LANG_EN = "en";


country=COUNTRY_EN ;
lang=LANG_EN ;
Locale myLocale = new Locale(lang, country);
Locale.setDefault(myLocale);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.setLayoutDirection(myLocale);
conf.locale = myLocale;
res.updateConfiguration(conf, dm);


来源:https://stackoverflow.com/questions/27164579/locale-not-set-programatically-in-android-5-0-lollipop

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