4

In the onCreate on the Application class of my app I have this code

 Locale current = getResources().getConfiguration().locale;
        String language = Utils.getPhoneLanguage();
        String text = getString(R.string.text);

I've set the phone language to german and

current is de_DE

language is de

And text is the string from values/strings.xml instead of values-de/strings.xml.

What can be the problem?

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • you don't have `text` inside `values-de/strings.xml` or you are changing your phone's locale programmatically – Blackbelt May 23 '16 at 08:22
  • I have it, and I don't change my phone locale programatically, these 3 lines are the first ones in onCreate of the application class. – Boldijar Paul May 23 '16 at 08:42
  • clean and rebuild your code – Blackbelt May 23 '16 at 08:43
  • @Blackbelt why is changing locale programmatically a problem? What is the correct way to change language of the application on demand? – Aykhan Hagverdili Aug 10 '21 at 11:32
  • Not sure if anything changed since 2016 - ideally the user locale settings of the phone picks the app locale. That being said if you need to switch locale in the app, do it – Blackbelt Aug 10 '21 at 20:25

1 Answers1

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

pass the language to set

Rafal
  • 292
  • 2
  • 10
vishal
  • 542
  • 6
  • 12