Note that there are some explanatory texts on larger screens.

plurals
  1. PODetect the real system locale
    primarykey
    data
    text
    <p>I'm fighting with setting locales on an app.</p> <p>What I want is to have a switch button: I want to let the user to switch between the default system locale and a fixed language (for example "en").</p> <p>I got this little piece of code, taken from another ticket here on S.O. (thank you):</p> <pre><code>private static void setThisLocale(Locale locale) { Locale.setDefault(locale); android.content.res.Configuration config = new android.content.res.Configuration(); config.locale = locale; ctx.getResources().updateConfiguration(config, ctx.getResources().getDisplayMetrics()); Log.d("locale", "locale set: " + locale.getLanguage()); } </code></pre> <p>This function sets the locale and it looks to work.</p> <p>The problem is that the defaultLanguage, after this, remains set and I cannot detect anymore which was the original locale. So, I cannot go back! For example:</p> <pre><code> defaultLocale = ctx.getResources().getConfiguration().locale; </code></pre> <p>or</p> <pre><code> defaultLocale = Locale.getDefault(); </code></pre> <p>Will both return me "en", even if before changing it the default locale was... say... "it".</p> <p>Weird enough, it won't change even after a manual restart of the app, it looks that Android stores the default locale for any app. So even checking which is the default locale at the very app start does not work.</p> <p>The only way to go back to the original defaultLocale is to reinstall the app, and at that point the button, for one single time, allows me to switch between the original default locale and "en", until it starts to alternate "en" and "en" forever again.</p> <p>From this, it looks like any app has a default locale stored, although I was not aware of it.</p> <p>Of course a bad programming patch could be to memorize the default locale only if I find it different from "en", but this wouldn't detect the real system locale, I would like to find something better.</p> <p>Is there any way to detect which is the REAL default locale of the SYSTEM, not just the latest default locale set for the app?</p> <p><strong>Found it. THE CORRECT ANSWER IS:</strong></p> <pre><code>defaultLocale = Resources.getSystem().getConfiguration().locale; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload