From e95cde5aa6cd50cc8c04176fbd4e9c3b6f602449 Mon Sep 17 00:00:00 2001 From: Jon Carifio Date: Wed, 27 Sep 2023 00:36:06 -0400 Subject: [PATCH 1/2] If the user doesn't have Portuguese installed, use English as the spell parsing locale. --- app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java | 7 +++++++ app/src/main/java/dnd/jon/spellbook/SettingsFragment.java | 5 ++--- .../main/java/dnd/jon/spellbook/SpellbookViewModel.java | 6 +++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java b/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java index cd78ff1a..0e997956 100644 --- a/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java +++ b/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java @@ -90,4 +90,11 @@ private static > int[] supportedIDs(E[] items, Map static int[] supportedTableLayoutIDs() { return supportedIDs(supportedClasses(), tableLayoutIDs); } static int[] supportedSpellcastingInfoIDs() { return supportedIDs(supportedClasses(), spellcastingInfoIDs); } + // TODO: Think of a way to make this more generic for other languages + static boolean hasPortugueseInstalled() { + final LocaleList localeList = LocaleList.getDefault(); + final Locale ptLocale = localeList.getFirstMatch(new String[]{"pt-BR", "pt-PT"}); + return ptLocale.getLanguage().equals("pt"); + } + } diff --git a/app/src/main/java/dnd/jon/spellbook/SettingsFragment.java b/app/src/main/java/dnd/jon/spellbook/SettingsFragment.java index 3045f419..9c3ded79 100644 --- a/app/src/main/java/dnd/jon/spellbook/SettingsFragment.java +++ b/app/src/main/java/dnd/jon/spellbook/SettingsFragment.java @@ -40,12 +40,11 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { } // Enable the ability to change the spell language, if applicable - final LocaleList localeList = LocaleList.getDefault(); - final Locale ptLocale = localeList.getFirstMatch(new String[]{"pt-BR", "pt-PT"}); + final boolean hasPortuguese = LocalizationUtils.hasPortugueseInstalled(); final PreferenceScreen preferenceScreen = getPreferenceScreen(); final Preference languagePreference = preferenceScreen.findPreference(getString(R.string.spell_language_key)); if (languagePreference != null) { - if (ptLocale.getLanguage().equals("pt")) { + if (hasPortuguese) { languagePreference.setVisible(true); languagePreference.setEnabled(true); } else { diff --git a/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java b/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java index da60b2f2..80095b49 100644 --- a/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java +++ b/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java @@ -105,7 +105,11 @@ public SpellbookViewModel(Application application) { final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application); final String spellLanguageKey = application.getString(R.string.spell_language_key); final String spellsLocaleString = sharedPreferences.getString(spellLanguageKey, null); - this.spellsLocale = spellsLocaleString == null ? LocalizationUtils.defaultSpellLocale() : new Locale(spellsLocaleString); + if (LocalizationUtils.hasPortugueseInstalled()) { + this.spellsLocale = spellsLocaleString == null ? LocalizationUtils.defaultSpellLocale() : new Locale(spellsLocaleString); + } else { + this.spellsLocale = Locale.US; + } // If we don't have an existing value for the spell language setting // we set the default. From 8e1396a71b76cb23709289fb99216832dbb19493 Mon Sep 17 00:00:00 2001 From: Jon Carifio Date: Wed, 27 Sep 2023 14:06:51 -0400 Subject: [PATCH 2/2] More updates to language-checking logic. --- app/build.gradle | 2 +- .../dnd/jon/spellbook/LocalizationUtils.java | 22 ++++++++++++++----- .../dnd/jon/spellbook/SpellbookViewModel.java | 13 ++++++++--- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 67f69b71..3c58cbc2 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,7 +15,7 @@ android { applicationId "dnd.jon.spellbook" minSdkVersion 24 targetSdkVersion 33 - versionCode 300300 + versionCode 3003001 versionName "3.3.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" signingConfig signingConfigs.release diff --git a/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java b/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java index 0e997956..d27f547b 100644 --- a/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java +++ b/app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java @@ -57,13 +57,17 @@ static Locale getLocale() { static Locale defaultSpellLocale() { final Locale locale = getLocale(); final String language = locale.getLanguage(); - if (supportedLanguages.contains(language)) { + if (isLanguageSupported(language)) { return locale; } else { return Locale.US; } } + static boolean isLanguageSupported(String languageCode) { + return supportedLanguages.contains(languageCode); + } + static String getCurrentLanguage() { return getLocale().getLanguage(); } @@ -90,11 +94,19 @@ private static > int[] supportedIDs(E[] items, Map static int[] supportedTableLayoutIDs() { return supportedIDs(supportedClasses(), tableLayoutIDs); } static int[] supportedSpellcastingInfoIDs() { return supportedIDs(supportedClasses(), spellcastingInfoIDs); } - // TODO: Think of a way to make this more generic for other languages - static boolean hasPortugueseInstalled() { + // BCP language codes are in BCP 47 format + // https://appmakers.dev/bcp-47-language-codes-list/ + static boolean hasOneInstalled(String[] bcpLanguageCodes, String languageCode) { final LocaleList localeList = LocaleList.getDefault(); - final Locale ptLocale = localeList.getFirstMatch(new String[]{"pt-BR", "pt-PT"}); - return ptLocale.getLanguage().equals("pt"); + final Locale locale = localeList.getFirstMatch(bcpLanguageCodes); + return locale.getLanguage().equals(languageCode); + } + static boolean hasEnglishInstalled() { + return hasOneInstalled(new String[]{"en-AU", "en-GB", "en-IE", "en-US", "en-ZA"}, "en"); + } + + static boolean hasPortugueseInstalled() { + return hasOneInstalled(new String[]{"pt-BR", "pt-PT"}, "pt"); } } diff --git a/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java b/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java index 80095b49..48bdd93b 100644 --- a/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java +++ b/app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java @@ -105,10 +105,17 @@ public SpellbookViewModel(Application application) { final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application); final String spellLanguageKey = application.getString(R.string.spell_language_key); final String spellsLocaleString = sharedPreferences.getString(spellLanguageKey, null); - if (LocalizationUtils.hasPortugueseInstalled()) { - this.spellsLocale = spellsLocaleString == null ? LocalizationUtils.defaultSpellLocale() : new Locale(spellsLocaleString); + + // This is kinda hacky; think of a more scalable way to do this? + // Though once the UI and spell parsing languages a + final boolean uninstalledLanguage = (spellsLocaleString == null) || + (spellsLocaleString.equals("pt") && !LocalizationUtils.hasPortugueseInstalled()) || + (spellsLocaleString.equals("en") && !LocalizationUtils.hasEnglishInstalled()); + + if (uninstalledLanguage || !LocalizationUtils.isLanguageSupported(spellsLocaleString)) { + this.spellsLocale = LocalizationUtils.defaultSpellLocale(); } else { - this.spellsLocale = Locale.US; + this.spellsLocale = new Locale(spellsLocaleString); } // If we don't have an existing value for the spell language setting