1

I am writing an app that has an option menu, with three options for language : automatic, english , greek.

When the user changes the setting, I save it in shared preferences, and call my starting activity with the new task flag

when that activity starts (before even setContentView) I call the following method:

        string language = MainApplication.Language; //this is from shared preferences
        if (language == null || language.Equals("auto")) {
            language = Java.Util.Locale.Default.Language; //if language is not set, or the setting is auto then load the default language
            MainApplication.Language = "auto"; //save into the shared preferences that the language was set to auto
        }
        App.instance.set_language(language); //update the data layer's language
        Java.Util.Locale locale = new Java.Util.Locale(language); 
        Java.Util.Locale.Default = locale; //change the default locale
        Configuration config = new Configuration();//create a new configuration and set its locale
        config.Locale = locale;
        Application.Context.Resources.UpdateConfiguration(config, null);//update the system locale

The above is written in Xamarin, so it's C# but it's the same code for java

My problem is that if I change the language to greek for example, when the code above runs it sets the default locale to "el" , but if the user selects the automatic option after that , because the default locale for the application is "el" it preserves that setting, even though my system language is english.

when I restart the application it works correctly because the application's default locale is that of the system

so my question is, is there a way to get the system's locale and not the applications?

p.s. I know that I can always store the system's locale in Application and change it in OnConfigurationChanged ,I was just wandering if there is another way

Cruces
  • 3,029
  • 1
  • 26
  • 55

1 Answers1

5

You can get it by adb shell getprop | grep locale

It will print similar output like:

[persist.sys.locale]: [tr-TR]
[ro.product.locale]: [tr-TR]

So you can run a process as:

Process process = Runtime.getRunTime().exec("getprop");

and check the output string with a BufferedReader.

or basically check this answer.

Community
  • 1
  • 1
Burak Day
  • 907
  • 14
  • 28