From cc7d0fc8f8e4f907b2936502ef67a56319f99ac3 Mon Sep 17 00:00:00 2001 From: JuRoot Date: Sun, 2 Jan 2022 17:04:42 +0100 Subject: [PATCH 1/4] main settings screen locale issue fixed setting locale before loading main settings view resources --- .../whereyougo/gui/activity/XmlSettingsActivity.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java b/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java index 1c26808ac..8289bbe9a 100644 --- a/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java +++ b/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java @@ -35,15 +35,16 @@ public class XmlSettingsActivity @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.layout_settings); - setTitle(R.string.settings); - ((TextView) findViewById(R.id.title_text)).setText(R.string.settings); - // Set language SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String lang = sharedPreferences.getString(getString(R.string.pref_KEY_S_LANGUAGE), ""); setLocale(this, lang); + setContentView(R.layout.layout_settings); + setTitle(R.string.settings); + ((TextView) findViewById(R.id.title_text)).setText(R.string.settings); + + /* workaround: I don't really know why I cannot call CustomActivity.customOnCreate(this); - OMG! */ switch (Preferences.APPEARANCE_FONT_SIZE) { case PreferenceValues.VALUE_FONT_SIZE_SMALL: From 0c7abf0c9a10fd849138c5547815a216e451d7ce Mon Sep 17 00:00:00 2001 From: JuRoot Date: Sun, 2 Jan 2022 17:06:18 +0100 Subject: [PATCH 2/4] better handling of default system language explicitly defined system language selection on default language choice, added code for better compatibility with APIs < 24 --- .../gui/extension/activity/CustomActivity.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java b/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java index 8fccdc693..53325aec3 100644 --- a/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java +++ b/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java @@ -1,17 +1,17 @@ /* * This file is part of WhereYouGo. - * + * * WhereYouGo is free software: you can redistribute it and/or modify it under the terms of the GNU * General Public License as published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * WhereYouGo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along with WhereYouGo. If not, * see . - * + * * Copyright (C) 2012 Menion */ @@ -54,6 +54,7 @@ protected void customOnCreate(Activity activity) { // Set language SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String lang = sharedPreferences.getString(getString(R.string.pref_KEY_S_LANGUAGE), ""); + Logger.v(TAG, "customOnCreate(), lang: " + lang); setLocale(this, lang); // set main activity parameters @@ -252,11 +253,16 @@ private void unbindDrawables(View view) { } public static void setLocale(Activity activity, String languageCode) { + if (languageCode.equals("default")) { + languageCode = Locale.getDefault().getLanguage(); + } Locale locale = new Locale(languageCode); Locale.setDefault(locale); Resources resources = activity.getResources(); Configuration config = resources.getConfiguration(); config.setLocale(locale); resources.updateConfiguration(config, resources.getDisplayMetrics()); + config.locale = locale; + activity.getApplicationContext().createConfigurationContext(config); } } From 3cdf75b22219759bb6dae3456b37b67381488d35 Mon Sep 17 00:00:00 2001 From: JuRoot Date: Sun, 2 Jan 2022 17:21:25 +0100 Subject: [PATCH 3/4] hotfix for (#329) making codacy happy --- .../whereyougo/gui/extension/activity/CustomActivity.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java b/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java index 53325aec3..bd99ca2d0 100644 --- a/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java +++ b/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java @@ -253,10 +253,11 @@ private void unbindDrawables(View view) { } public static void setLocale(Activity activity, String languageCode) { + String lang = languageCode; if (languageCode.equals("default")) { - languageCode = Locale.getDefault().getLanguage(); + lang = Locale.getDefault().getLanguage(); } - Locale locale = new Locale(languageCode); + Locale locale = new Locale(lang); Locale.setDefault(locale); Resources resources = activity.getResources(); Configuration config = resources.getConfiguration(); From 968192a7e92d62b3e7f78396a9364befcb2a5590 Mon Sep 17 00:00:00 2001 From: JuRoot Date: Mon, 3 Jan 2022 00:32:09 +0100 Subject: [PATCH 4/4] hotfix for (#329) support for regional language codes little refactor of locale code, CustomActivity.setLocale now parses language for itself, even for system provided language code --- .../gui/activity/XmlSettingsActivity.java | 6 ++-- .../extension/activity/CustomActivity.java | 30 +++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java b/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java index 8289bbe9a..a53639e31 100644 --- a/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java +++ b/src/main/java/menion/android/whereyougo/gui/activity/XmlSettingsActivity.java @@ -16,13 +16,12 @@ import androidx.preference.PreferenceManager; import menion.android.whereyougo.R; +import menion.android.whereyougo.gui.extension.activity.CustomActivity; import menion.android.whereyougo.preferences.PreferenceValues; import menion.android.whereyougo.preferences.Preferences; import menion.android.whereyougo.utils.Logger; import menion.android.whereyougo.utils.Utils; -import static menion.android.whereyougo.gui.extension.activity.CustomActivity.setLocale; - public class XmlSettingsActivity extends AppCompatActivity @@ -38,13 +37,12 @@ public void onCreate(Bundle savedInstanceState) { // Set language SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String lang = sharedPreferences.getString(getString(R.string.pref_KEY_S_LANGUAGE), ""); - setLocale(this, lang); + CustomActivity.setLocale(this, lang); setContentView(R.layout.layout_settings); setTitle(R.string.settings); ((TextView) findViewById(R.id.title_text)).setText(R.string.settings); - /* workaround: I don't really know why I cannot call CustomActivity.customOnCreate(this); - OMG! */ switch (Preferences.APPEARANCE_FONT_SIZE) { case PreferenceValues.VALUE_FONT_SIZE_SMALL: diff --git a/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java b/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java index bd99ca2d0..a4e49be4a 100644 --- a/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java +++ b/src/main/java/menion/android/whereyougo/gui/extension/activity/CustomActivity.java @@ -253,14 +253,34 @@ private void unbindDrawables(View view) { } public static void setLocale(Activity activity, String languageCode) { - String lang = languageCode; + Locale locale; + String lang; + Resources resources = activity.getResources(); + Configuration config = resources.getConfiguration(); + + // we need to get language and region first even for default to correctly initialize Locale if (languageCode.equals("default")) { - lang = Locale.getDefault().getLanguage(); + lang = Locale.getDefault().toString(); + } else { + lang = new Locale(languageCode).toString(); + } + + // need to parse language codes with dash and hyphen + String[] loc = lang.split("[-_]"); + if (loc.length == 1) { + if (loc[0].equals("pt")) { + // nasty hotfix for Portuguese language selected without regional code + locale = new Locale(loc[0], loc[0]); + } else { + locale = new Locale(lang); + } + } else if (loc.length == 2) { + locale = new Locale(loc[0], loc[1]); + } else { + locale = config.locale; } - Locale locale = new Locale(lang); + Locale.setDefault(locale); - Resources resources = activity.getResources(); - Configuration config = resources.getConfiguration(); config.setLocale(locale); resources.updateConfiguration(config, resources.getDisplayMetrics()); config.locale = locale;