Add preference to change locale

This commit is contained in:
Victor Shcherb 2011-05-08 22:48:54 +02:00
parent d31d02491b
commit 1fb99684ea
6 changed files with 68 additions and 2 deletions

View file

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="system_locale">Системная</string>
<string name="preferred_locale_descr">Изменить язык программы</string>
<string name="preferred_locale">Локализация</string>
<string name="tip_map_switch">Источник карты</string> <string name="tip_map_switch">Источник карты</string>
<string name="tip_map_switch_t">Наиболее быстрый способ изменить источник карты и слои \'Меню\'->\'Слои\'. <string name="tip_map_switch_t">Наиболее быстрый способ изменить источник карты и слои \'Меню\'->\'Слои\'.
\n\tПод \'Карта источник...\' вы можете выбрать из предопределенных типов или собственных, созданных OsmAndMapCreator на PC. \n\tПод \'Карта источник...\' вы можете выбрать из предопределенных типов или собственных, созданных OsmAndMapCreator на PC.

View file

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources> <resources>
<string name="system_locale">System</string>
<string name="preferred_locale_descr">Change display language</string>
<string name="preferred_locale">Preferred locale</string>
<string name="tip_map_switch">Map source</string> <string name="tip_map_switch">Map source</string>
<string name="tip_map_switch_t">The fastest way to change map source and layers is pressing \'Menu\'->\'Layers\' on map. <string name="tip_map_switch_t">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. \n\tUnder \'Map source...\' you can choose predefined tile sources or manually created using OsmAndMapCreator on PC.

View file

@ -53,6 +53,7 @@
<EditTextPreference android:title="@string/application_dir" android:key="external_storage_dir"></EditTextPreference> <EditTextPreference android:title="@string/application_dir" android:key="external_storage_dir"></EditTextPreference>
<ListPreference android:key="default_metric_system" android:title="@string/unit_of_length" android:summary="@string/unit_of_length_descr"></ListPreference> <ListPreference android:key="default_metric_system" android:title="@string/unit_of_length" android:summary="@string/unit_of_length_descr"></ListPreference>
<ListPreference android:key="position_on_map" android:title="@string/position_on_map" android:summary="@string/position_on_map_descr"></ListPreference> <ListPreference android:key="position_on_map" android:title="@string/position_on_map" android:summary="@string/position_on_map_descr"></ListPreference>
<ListPreference android:key="preferred_locale" android:title="@string/preferred_locale" android:summary="@string/preferred_locale_descr"></ListPreference>
<CheckBoxPreference android:key="use_trackball_for_movements" android:title="@string/use_trackball" android:summary="@string/use_trackball_descr"></CheckBoxPreference> <CheckBoxPreference android:key="use_trackball_for_movements" android:title="@string/use_trackball" android:summary="@string/use_trackball_descr"></CheckBoxPreference>
</PreferenceScreen> </PreferenceScreen>

View file

@ -233,11 +233,21 @@ public class OsmandSettings {
return prefs.edit().putBoolean(SHOW_TRANSPORT_OVER_MAP, val).commit(); 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 // 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 = "user_name"; //$NON-NLS-1$
public static final String USER_NAME_DEF = "NoName"; //$NON-NLS-1$
public static String getUserName(SharedPreferences prefs) { 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) { public static boolean setUserName(Context ctx, String name) {

View file

@ -7,6 +7,7 @@ import java.io.FileWriter;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.util.List; import java.util.List;
import java.util.Locale;
import net.osmand.Algoritms; import net.osmand.Algoritms;
import net.osmand.LogUtil; import net.osmand.LogUtil;
@ -25,6 +26,8 @@ import android.app.AlertDialog.Builder;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Handler; import android.os.Handler;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.Log; import android.util.Log;
@ -48,6 +51,7 @@ public class OsmandApplication extends Application {
private DayNightHelper daynightHelper; private DayNightHelper daynightHelper;
private NavigationService navigationService; private NavigationService navigationService;
private boolean applicationInitializing = false; private boolean applicationInitializing = false;
private Locale prefferedLocale = null;
public void onCreate(){ public void onCreate(){
@ -56,6 +60,7 @@ public class OsmandApplication extends Application {
manager = new ResourceManager(this); manager = new ResourceManager(this);
daynightHelper = new DayNightHelper(this); daynightHelper = new DayNightHelper(this);
uiHandler = new Handler(); uiHandler = new Handler();
checkPrefferedLocale();
startApplication(); startApplication();
} }
@ -87,6 +92,29 @@ public class OsmandApplication extends Application {
manager.onLowMemory(); 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){ public ProgressDialog checkApplicationIsBeingInitialized(Context uiContext){
// start application if it was previously closed // start application if it was previously closed
startApplication(); startApplication();

View file

@ -99,6 +99,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
private ListPreference mapScreenOrientation; private ListPreference mapScreenOrientation;
private ListPreference voicePreference; private ListPreference voicePreference;
private ListPreference metricPreference; private ListPreference metricPreference;
private ListPreference preferredLocale;
private ListPreference rendererPreference; private ListPreference rendererPreference;
private ListPreference routeServiceInterval; private ListPreference routeServiceInterval;
private ListPreference routeServiceWaitInterval; private ListPreference routeServiceWaitInterval;
@ -176,6 +177,8 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
voicePreference.setOnPreferenceChangeListener(this); voicePreference.setOnPreferenceChangeListener(this);
metricPreference = (ListPreference) screen.findPreference(OsmandSettings.DEFAULT_METRIC_SYSTEM); metricPreference = (ListPreference) screen.findPreference(OsmandSettings.DEFAULT_METRIC_SYSTEM);
metricPreference.setOnPreferenceChangeListener(this); metricPreference.setOnPreferenceChangeListener(this);
preferredLocale = (ListPreference) screen.findPreference(OsmandSettings.PREFERRED_LOCALE);
preferredLocale.setOnPreferenceChangeListener(this);
rendererPreference =(ListPreference) screen.findPreference(OsmandSettings.RENDERER); rendererPreference =(ListPreference) screen.findPreference(OsmandSettings.RENDERER);
rendererPreference.setOnPreferenceChangeListener(this); rendererPreference.setOnPreferenceChangeListener(this);
routeServiceInterval =(ListPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_INTERVAL); routeServiceInterval =(ListPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_INTERVAL);
@ -315,6 +318,16 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
} }
fill(metricPreference, names, values, entry); 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 // read available voice data
File extStorage = OsmandSettings.extendOsmandPath(getApplicationContext(), ResourceManager.VOICE_PATH); File extStorage = OsmandSettings.extendOsmandPath(getApplicationContext(), ResourceManager.VOICE_PATH);
Set<String> setFiles = new LinkedHashSet<String>(); Set<String> setFiles = new LinkedHashSet<String>();
@ -539,6 +552,14 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
} else if (preference == metricPreference) { } else if (preference == metricPreference) {
MetricsConstants mc = MetricsConstants.valueOf((String) newValue); MetricsConstants mc = MetricsConstants.valueOf((String) newValue);
OsmandSettings.setDefaultMetricConstants(edit, mc); 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) { } else if (preference == tileSourcePreference) {
if(VECTOR_MAP.equals((String) newValue)){ if(VECTOR_MAP.equals((String) newValue)){
edit.putBoolean(OsmandSettings.MAP_VECTOR_DATA, true); edit.putBoolean(OsmandSettings.MAP_VECTOR_DATA, true);