From 8e1396a71b76cb23709289fb99216832dbb19493 Mon Sep 17 00:00:00 2001 From: Jon Carifio Date: Wed, 27 Sep 2023 14:06:51 -0400 Subject: [PATCH] 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