Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore an Android app's default locale

Tags:

android

locale

I have this Android app that's localized in several languages. If the appropriate string for the device's locale exists, the app automatically displays it. So far, so good.

Next, I want to allow the app's users to switch to one of the languages I've provided. I can do this by overriding the default locale. No problem here, either.

The question here: Is there a way for me to provide a "default" option in the language list that would allow users to restore the default, automatic locale? Consider the following scenario:
1. Device's locale is set to German. App displays everything in German.
2. User wants the app to be in French, so they change the language from inside the app, and the app turns French as a result.
3. The same user changes the device's locale to Spanish. App is still French.
4. They select the "Default" setting. The app changes to Spanish, and keeps changing when the locale is changed.

If this isn't doable automatically, then is there a way for me to get the device-wide locale so that I can set my app to it if my "default" option is selected?

like image 733
Goldsmith Avatar asked Apr 01 '11 11:04

Goldsmith


1 Answers

I resolved this issue by storing the existing setting on first run of the app (before any changes are made to the default locale). In a block that only runs the first time my app runs, I call:

m_preferences.edit().putString(
                        MyConstants.PREFERENCE_SYSTEM_LANGUAGE,
                        getResources().getConfiguration().locale.getLanguage()
                        ).commit();

Later, when I need to retrieve it:

languageToLoad = m_preferences.getString(
                        MyConstants.PREFERENCE_SYSTEM_LANGUAGE,
                        MyConstants.LANGUAGE_DEFAULT);

When changing the language, I save that in another preference and handle the updates to the view.

Edit: After poking around a bit more, I found the system setting. I switched to using this, rather than the above code.

Resources.getSystem().getConfiguration().locale.getLanguage();
like image 59
ProjectJourneyman Avatar answered Nov 15 '22 18:11

ProjectJourneyman