From 1fb99684eae969052e85585010172e3b722795e7 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 8 May 2011 22:48:54 +0200 Subject: [PATCH] Add preference to change locale --- OsmAnd/res/values-ru/strings.xml | 3 ++ OsmAnd/res/values/strings.xml | 3 ++ OsmAnd/res/xml/settings_pref.xml | 1 + .../src/net/osmand/plus/OsmandSettings.java | 12 +++++++- .../plus/activities/OsmandApplication.java | 30 ++++++++++++++++++- .../plus/activities/SettingsActivity.java | 21 +++++++++++++ 6 files changed, 68 insertions(+), 2 deletions(-) diff --git a/OsmAnd/res/values-ru/strings.xml b/OsmAnd/res/values-ru/strings.xml index ec86b52ce3..1b57431c1e 100644 --- a/OsmAnd/res/values-ru/strings.xml +++ b/OsmAnd/res/values-ru/strings.xml @@ -1,5 +1,8 @@ + Системная + Изменить язык программы + Локализация Источник карты Наиболее быстрый способ изменить источник карты и слои \'Меню\'->\'Слои\'. \n\tПод \'Карта источник...\' вы можете выбрать из предопределенных типов или собственных, созданных OsmAndMapCreator на PC. diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 0319d00e1c..5f781ff248 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1,5 +1,8 @@ + System + Change display language + Preferred locale Map source The fastest way to change map source and layers is pressing \'Menu\'->\'Layers\' on map. \n\tUnder \'Map source...\' you can choose predefined tile sources or manually created using OsmAndMapCreator on PC. diff --git a/OsmAnd/res/xml/settings_pref.xml b/OsmAnd/res/xml/settings_pref.xml index fa4033d222..2e40de7229 100644 --- a/OsmAnd/res/xml/settings_pref.xml +++ b/OsmAnd/res/xml/settings_pref.xml @@ -53,6 +53,7 @@ + diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 8c0c3c0836..1f5aa4ac12 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -232,12 +232,22 @@ public class OsmandSettings { SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); return prefs.edit().putBoolean(SHOW_TRANSPORT_OVER_MAP, val).commit(); } + + + // this value string is synchronized with settings_pref.xml preference name + public static final String PREFERRED_LOCALE = "preferred_locale"; //$NON-NLS-1$ + public static final String PREFERRED_LOCALE_DEF = ""; //$NON-NLS-1$ + + public static String getPreferredLocale(SharedPreferences prefs) { + return prefs.getString(PREFERRED_LOCALE, PREFERRED_LOCALE_DEF); //$NON-NLS-1$ + } // this value string is synchronized with settings_pref.xml preference name public static final String USER_NAME = "user_name"; //$NON-NLS-1$ + public static final String USER_NAME_DEF = "NoName"; //$NON-NLS-1$ public static String getUserName(SharedPreferences prefs) { - return prefs.getString(USER_NAME, "NoName"); //$NON-NLS-1$ + return prefs.getString(USER_NAME, USER_NAME_DEF); } public static boolean setUserName(Context ctx, String name) { diff --git a/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java index 16186fcd37..fbf22aacfc 100644 --- a/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/activities/OsmandApplication.java @@ -7,6 +7,7 @@ import java.io.FileWriter; import java.io.PrintStream; import java.lang.Thread.UncaughtExceptionHandler; import java.util.List; +import java.util.Locale; import net.osmand.Algoritms; import net.osmand.LogUtil; @@ -25,6 +26,8 @@ import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Configuration; import android.os.Handler; import android.text.format.DateFormat; import android.util.Log; @@ -48,6 +51,7 @@ public class OsmandApplication extends Application { private DayNightHelper daynightHelper; private NavigationService navigationService; private boolean applicationInitializing = false; + private Locale prefferedLocale = null; public void onCreate(){ @@ -56,10 +60,11 @@ public class OsmandApplication extends Application { manager = new ResourceManager(this); daynightHelper = new DayNightHelper(this); uiHandler = new Handler(); + checkPrefferedLocale(); startApplication(); } - public PoiFiltersHelper getPoiFilters() { + public PoiFiltersHelper getPoiFilters() { if(poiFilters == null){ poiFilters = new PoiFiltersHelper(this); } @@ -87,6 +92,29 @@ public class OsmandApplication extends Application { manager.onLowMemory(); } + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + if (prefferedLocale != null) { + newConfig.locale = prefferedLocale; + Locale.setDefault(prefferedLocale); + getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics()); + } + } + + public void checkPrefferedLocale() { + SharedPreferences settings = OsmandSettings.getSharedPreferences(this); + Configuration config = getBaseContext().getResources().getConfiguration(); + String lang = OsmandSettings.getPreferredLocale(settings); + if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) { + prefferedLocale = new Locale(lang); + Locale.setDefault(prefferedLocale); + config.locale = prefferedLocale; + getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); + } + + } + public ProgressDialog checkApplicationIsBeingInitialized(Context uiContext){ // start application if it was previously closed startApplication(); diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java index c4e383d8a4..543c75cfbc 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java @@ -99,6 +99,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference private ListPreference mapScreenOrientation; private ListPreference voicePreference; private ListPreference metricPreference; + private ListPreference preferredLocale; private ListPreference rendererPreference; private ListPreference routeServiceInterval; private ListPreference routeServiceWaitInterval; @@ -176,6 +177,8 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference voicePreference.setOnPreferenceChangeListener(this); metricPreference = (ListPreference) screen.findPreference(OsmandSettings.DEFAULT_METRIC_SYSTEM); metricPreference.setOnPreferenceChangeListener(this); + preferredLocale = (ListPreference) screen.findPreference(OsmandSettings.PREFERRED_LOCALE); + preferredLocale.setOnPreferenceChangeListener(this); rendererPreference =(ListPreference) screen.findPreference(OsmandSettings.RENDERER); rendererPreference.setOnPreferenceChangeListener(this); routeServiceInterval =(ListPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_INTERVAL); @@ -314,6 +317,16 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference names[i] = MetricsConstants.values()[i].toHumanString(this); } fill(metricPreference, names, values, entry); + + String[] locales = //getResources().getAssets().getLocales(); + new String [] { "en", "cs", "de", "es", "fr", "hu", "it", "pt", "ru", "sk"}; + values = new String[locales.length + 1]; + names = new String[locales.length + 1] ; + values[0] = OsmandSettings.PREFERRED_LOCALE_DEF; + names[0] = getString(R.string.system_locale); + System.arraycopy(locales, 0, names, 1, locales.length); + System.arraycopy(locales, 0, values, 1, locales.length); + fill(preferredLocale, names, values, OsmandSettings.getPreferredLocale(prefs)); // read available voice data File extStorage = OsmandSettings.extendOsmandPath(getApplicationContext(), ResourceManager.VOICE_PATH); @@ -539,6 +552,14 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference } else if (preference == metricPreference) { MetricsConstants mc = MetricsConstants.valueOf((String) newValue); OsmandSettings.setDefaultMetricConstants(edit, mc); + } else if (preference == preferredLocale) { + edit.putString(OsmandSettings.PREFERRED_LOCALE, (String) newValue); + edit.commit(); + // restart activity + getMyApplication().checkPrefferedLocale(); + Intent intent = getIntent(); + finish(); + startActivity(intent); } else if (preference == tileSourcePreference) { if(VECTOR_MAP.equals((String) newValue)){ edit.putBoolean(OsmandSettings.MAP_VECTOR_DATA, true);