Skip to content

Commit

Permalink
Fixes Issue #252 where languages were null when updating
Browse files Browse the repository at this point in the history
When updating with the MulliganRenamer window open, Resources failed
to load on the first frame. This meant languages were null and
requesting a translation failed to find anything.

This fix continues to load resources until they are loaded.
  • Loading branch information
edwardrowe committed May 12, 2020
1 parent 1d6753b commit 998b2fa
Showing 1 changed file with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public static LocalizationManager Instance
_Instance = new LocalizationManager();
}

// Issue #252 - When updating from 1.6 to 1.7 with window open, Resources would be null on first frame.
// This resulted in exception spam when trying to GetTranslation because no languages were loaded.
// Now we set a flag so that we will continue to reload until we get languages.
if (!_Instance.areLanguagesLoaded)
{
_Instance.Initialize();
}

return _Instance;
}
}
Expand All @@ -72,8 +80,11 @@ public List<Language> AllLanguages
}

private Language currentLanguage;

private List<Language> allLanguages;

private bool areLanguagesLoaded;

private LocalizationManager()
{
this.Initialize();
Expand Down Expand Up @@ -109,9 +120,13 @@ public static List<Language> LoadAllLanguages()
public void Initialize()
{
this.CacheAllLanguages();
var preferences = MulliganUserPreferences.LoadOrCreatePreferences();
this.ChangeLanguage(preferences.CurrentLanguageKey);
if (this.areLanguagesLoaded)
{
var preferences = MulliganUserPreferences.LoadOrCreatePreferences();
this.ChangeLanguage(preferences.CurrentLanguageKey);
}
}

/// <summary>
/// Change the current Locale so that Translations are of the new, specified languages
/// </summary>
Expand All @@ -128,6 +143,11 @@ public void ChangeLanguage(string languageKey)
this.LanguageChanged.Invoke();
}
}
else
{
throw new Exception("Language with key [" + languageKey + "] not found in LocalizationManager's loaded languages. " +
"Are you sure it's a valid key? Did ChangeLanguage get called bofore LocalizationManager could load languages?");
}
}

/// <summary>
Expand Down Expand Up @@ -162,17 +182,30 @@ public List<LanguageUpdateReport> AddOrUpdateLanguages(IEnumerable<Language> lan
/// <returns>The value stored at the key in the current language</returns>
public string GetTranslation(string languageKey)
{
if (this.currentLanguage == null)
if (!this.areLanguagesLoaded)
{
throw new Exception("Current Language is not set");
return string.Empty;
}

return this.currentLanguage.GetValue(languageKey);
if (this.currentLanguage != null)
{
return this.currentLanguage.GetValue(languageKey);
}
else
{
throw new Exception("CurrentLanguage is unset on LocalizationManager. Somehow we are requesting translations " +
"before LocalizationManager succesfully loaded languages.");
}
}

private void CacheAllLanguages()
{
this.areLanguagesLoaded = false;
this.allLanguages = LoadAllLanguages();
if (this.allLanguages != null && this.allLanguages.Count > 0)
{
this.areLanguagesLoaded = true;
}
}

private LanguageUpdateReport UpdateLanguage(Language newLanguage)
Expand Down

0 comments on commit 998b2fa

Please sign in to comment.