diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-08-25 21:54:00 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-08-25 21:54:00 -0400 |
commit | 211f89821e34bb55a6266384d9bac68ec0c64744 (patch) | |
tree | aa0d9915b01aad436ed6b1a2028602c048666457 /src/SMAPI/Framework/Translator.cs | |
parent | 80d3dd1f786f7e5846f9adb7f7a4d82e5b9b92fd (diff) | |
parent | 31ac964a8b19623b0472931403a33d51db6fb271 (diff) | |
download | SMAPI-211f89821e34bb55a6266384d9bac68ec0c64744.tar.gz SMAPI-211f89821e34bb55a6266384d9bac68ec0c64744.tar.bz2 SMAPI-211f89821e34bb55a6266384d9bac68ec0c64744.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Translator.cs')
-rw-r--r-- | src/SMAPI/Framework/Translator.cs | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/src/SMAPI/Framework/Translator.cs b/src/SMAPI/Framework/Translator.cs index 11ec983b..4492b17f 100644 --- a/src/SMAPI/Framework/Translator.cs +++ b/src/SMAPI/Framework/Translator.cs @@ -46,18 +46,10 @@ namespace StardewModdingAPI.Framework this.LocaleEnum = localeEnum; this.ForLocale = new Dictionary<string, Translation>(StringComparer.OrdinalIgnoreCase); - foreach (string next in this.GetRelevantLocales(this.Locale)) + foreach (string key in this.GetAllKeysRaw()) { - // skip if locale not defined - if (!this.All.TryGetValue(next, out IDictionary<string, string> translations)) - continue; - - // add missing translations - foreach (var pair in translations) - { - if (!this.ForLocale.ContainsKey(pair.Key)) - this.ForLocale.Add(pair.Key, new Translation(this.Locale, pair.Key, pair.Value)); - } + string text = this.GetRaw(key, locale, withFallback: true); + this.ForLocale.Add(key, new Translation(this.Locale, key, text)); } } @@ -83,6 +75,25 @@ namespace StardewModdingAPI.Framework return this.Get(key).Tokens(tokens); } + /// <summary>Get a translation in every locale for which it's defined.</summary> + /// <param name="key">The translation key.</param> + /// <param name="withFallback">Whether to add duplicate translations for locale fallback. For example, if a translation is defined in <c>default.json</c> but not <c>fr.json</c>, setting this to true will add a <c>fr</c> entry which duplicates the default text.</param> + public IDictionary<string, Translation> GetInAllLocales(string key, bool withFallback) + { + IDictionary<string, Translation> translations = new Dictionary<string, Translation>(); + + foreach (var localeSet in this.All) + { + string locale = localeSet.Key; + string text = this.GetRaw(key, locale, withFallback); + + if (text != null) + translations[locale] = new Translation(locale, key, text); + } + + return translations; + } + /// <summary>Set the translations to use.</summary> /// <param name="translations">The translations to use.</param> internal Translator SetTranslations(IDictionary<string, IDictionary<string, string>> translations) @@ -102,6 +113,38 @@ namespace StardewModdingAPI.Framework /********* ** Private methods *********/ + /// <summary>Get all translation keys in the underlying translation data, ignoring the <see cref="ForLocale"/> cache.</summary> + private IEnumerable<string> GetAllKeysRaw() + { + return new HashSet<string>( + this.All.SelectMany(p => p.Value.Keys), + StringComparer.OrdinalIgnoreCase + ); + } + + /// <summary>Get a translation from the underlying translation data, ignoring the <see cref="ForLocale"/> cache.</summary> + /// <param name="key">The translation key.</param> + /// <param name="locale">The locale to get.</param> + /// <param name="withFallback">Whether to add duplicate translations for locale fallback. For example, if a translation is defined in <c>default.json</c> but not <c>fr.json</c>, setting this to true will add a <c>fr</c> entry which duplicates the default text.</param> + private string GetRaw(string key, string locale, bool withFallback) + { + foreach (string next in this.GetRelevantLocales(locale)) + { + string translation = null; + bool hasTranslation = + this.All.TryGetValue(next, out IDictionary<string, string> translations) + && translations.TryGetValue(key, out translation); + + if (hasTranslation) + return translation; + + if (!withFallback) + break; + } + + return null; + } + /// <summary>Get the locales which can provide translations for the given locale, in precedence order.</summary> /// <param name="locale">The locale for which to find valid locales.</param> private IEnumerable<string> GetRelevantLocales(string locale) |