I got a minor problem about app localization:
Here's the case - I have given my users an option to always use the application in Norwegian, regardless of system language.
It works just fine for the most part (I go to settings, check the box to force Norwegian, press "back" and the previous activity is displayed in Norwegian - same thing "the other way around"), however - the language change only seems to correctly update (reload resources) for the first activity in my "back stack".
To illustrate a typical scenario:
User launches the app, and is presented with the main activity (in English). From there, he selects the second activity (also in English). He then goes into settings (from the menu in the second activity) and sets the preference to force Norwegian.
When he then navigates back, the second activity is correctly updated and displayed in Norwegian (so far so good). However, when he presses "back" once more to return to the main activity, it is still displayed in English...
If he goes back out and launches the app again, the main activity is correctly displayed in Norwegian...
Are there any bright minds here with a suggestion to what I should do?
Relevant source code:
Code included in every activity to set the display language:
In onCreate: Globals.locale_default = Locale.getDefault().getDisplayLanguage();
public void onStart() {
super.onStart();
forceNorwegian = settings.getBoolean(getString(R.string.pref_key_forceNorwegian).toString(), false);
if (forceNorwegian) {
languageCheck("no");
} else {
Globals.locale = null;
languageCheck(Globals.locale_default);
}
}
public void languageCheck(String lang) {
Globals.locale = new Locale( lang );
// Check the current system locale and change it to Norwegian if it's not already the default
Globals.checkLocale( this );
if (Globals.language_changed) {
// Restart activity
Intent restart = getIntent();
finish();
Globals.language_changed = false;
startActivity(restart);
}
}
Globals.java:
public class Globals {
public static Locale locale = null;
public static String locale_default = null;
public static boolean language_changed = false;
public static void checkLocale( Activity a ) {
if( locale == null )
return;
Configuration config = a.getBaseContext().getResources().getConfiguration();
if( !config.locale.equals( locale ) )
{ // Change to the new locale. Everything will need to be closed or reloaded.
config.locale = locale;
a.getBaseContext().getResources().updateConfiguration( config, null );
language_changed = true;
}
}
}