Skip to content

Commit

Permalink
Merge pull request #37 from Carifio24/spell-locale-check-pt
Browse files Browse the repository at this point in the history
Additional conditional logic for setting spell locale
  • Loading branch information
Carifio24 authored Sep 28, 2023
2 parents f73f856 + 8e1396a commit a4e98da
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 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
21 changes: 20 additions & 1 deletion 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,4 +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); }

// 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 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");
}

}
5 changes: 2 additions & 3 deletions app/src/main/java/dnd/jon/spellbook/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,18 @@ 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);

// 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 = new Locale(spellsLocaleString);
}

// If we don't have an existing value for the spell language setting
// we set the default.
Expand Down

0 comments on commit a4e98da

Please sign in to comment.