Skip to content

Commit

Permalink
More updates to language-checking logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 27, 2023
1 parent e95cde5 commit 8e1396a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -90,11 +94,19 @@ private static <E extends Enum<E>> int[] supportedIDs(E[] items, Map<E,Integer>
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");
}

}
13 changes: 10 additions & 3 deletions app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e1396a

Please sign in to comment.