diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI.Tests/Core/TranslationTests.cs | 49 | ||||
-rw-r--r-- | src/SMAPI/Framework/ModHelpers/TranslationHelper.cs | 13 | ||||
-rw-r--r-- | src/SMAPI/Framework/SCore.cs | 6 | ||||
-rw-r--r-- | src/SMAPI/Translation.cs | 55 |
4 files changed, 46 insertions, 77 deletions
diff --git a/src/SMAPI.Tests/Core/TranslationTests.cs b/src/SMAPI.Tests/Core/TranslationTests.cs index c098aca5..457f9fad 100644 --- a/src/SMAPI.Tests/Core/TranslationTests.cs +++ b/src/SMAPI.Tests/Core/TranslationTests.cs @@ -32,7 +32,7 @@ namespace SMAPI.Tests.Core var data = new Dictionary<string, IDictionary<string, string>>(); // act - ITranslationHelper helper = new TranslationHelper("ModID", "ModName", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); + ITranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); Translation translation = helper.Get("key"); Translation[] translationList = helper.GetTranslations()?.ToArray(); @@ -55,7 +55,7 @@ namespace SMAPI.Tests.Core // act var actual = new Dictionary<string, Translation[]>(); - TranslationHelper helper = new TranslationHelper("ModID", "ModName", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); + TranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); foreach (string locale in expected.Keys) { this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en); @@ -79,7 +79,7 @@ namespace SMAPI.Tests.Core // act var actual = new Dictionary<string, Translation[]>(); - TranslationHelper helper = new TranslationHelper("ModID", "ModName", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); + TranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data); foreach (string locale in expected.Keys) { this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en); @@ -109,14 +109,14 @@ namespace SMAPI.Tests.Core [TestCase(" boop ", ExpectedResult = true)] public bool Translation_HasValue(string text) { - return new Translation("ModName", "pt-BR", "key", text).HasValue(); + return new Translation("pt-BR", "key", text).HasValue(); } [Test(Description = "Assert that the translation's ToString method returns the expected text for various inputs.")] public void Translation_ToString([ValueSource(nameof(TranslationTests.Samples))] string text) { // act - Translation translation = new Translation("ModName", "pt-BR", "key", text); + Translation translation = new Translation("pt-BR", "key", text); // assert if (translation.HasValue()) @@ -129,7 +129,7 @@ namespace SMAPI.Tests.Core public void Translation_ImplicitStringConversion([ValueSource(nameof(TranslationTests.Samples))] string text) { // act - Translation translation = new Translation("ModName", "pt-BR", "key", text); + Translation translation = new Translation("pt-BR", "key", text); // assert if (translation.HasValue()) @@ -142,7 +142,7 @@ namespace SMAPI.Tests.Core public void Translation_UsePlaceholder([Values(true, false)] bool value, [ValueSource(nameof(TranslationTests.Samples))] string text) { // act - Translation translation = new Translation("ModName", "pt-BR", "key", text).UsePlaceholder(value); + Translation translation = new Translation("pt-BR", "key", text).UsePlaceholder(value); // assert if (translation.HasValue()) @@ -153,24 +153,11 @@ namespace SMAPI.Tests.Core Assert.AreEqual(this.GetPlaceholderText("key"), translation.ToString(), "The translation returned an unexpected value given a null or empty input with the placeholder enabled."); } - [Test(Description = "Assert that the translation's Assert method throws the expected exception.")] - public void Translation_Assert([ValueSource(nameof(TranslationTests.Samples))] string text) - { - // act - Translation translation = new Translation("ModName", "pt-BR", "key", text); - - // assert - if (translation.HasValue()) - Assert.That(() => translation.Assert(), Throws.Nothing, "The assert unexpected threw an exception for a valid input."); - else - Assert.That(() => translation.Assert(), Throws.Exception.TypeOf<KeyNotFoundException>(), "The assert didn't throw an exception for invalid input."); - } - [Test(Description = "Assert that the translation returns the expected text after setting the default.")] public void Translation_Default([ValueSource(nameof(TranslationTests.Samples))] string text, [ValueSource(nameof(TranslationTests.Samples))] string @default) { // act - Translation translation = new Translation("ModName", "pt-BR", "key", text).Default(@default); + Translation translation = new Translation("pt-BR", "key", text).Default(@default); // assert if (!string.IsNullOrEmpty(text)) @@ -195,7 +182,7 @@ namespace SMAPI.Tests.Core string expected = $"{start} tokens are properly replaced (including {middle} {middle}) {end}"; // act - Translation translation = new Translation("ModName", "pt-BR", "key", input); + Translation translation = new Translation("pt-BR", "key", input); switch (structure) { case "anonymous object": @@ -236,7 +223,7 @@ namespace SMAPI.Tests.Core string value = Guid.NewGuid().ToString("N"); // act - Translation translation = new Translation("ModName", "pt-BR", "key", text).Tokens(new Dictionary<string, object> { [key] = value }); + Translation translation = new Translation("pt-BR", "key", text).Tokens(new Dictionary<string, object> { [key] = value }); // assert Assert.AreEqual(value, translation.ToString(), "The translation returned an unexpected value given a valid base text."); @@ -252,7 +239,7 @@ namespace SMAPI.Tests.Core string value = Guid.NewGuid().ToString("N"); // act - Translation translation = new Translation("ModName", "pt-BR", "key", text).Tokens(new Dictionary<string, object> { [key] = value }); + Translation translation = new Translation("pt-BR", "key", text).Tokens(new Dictionary<string, object> { [key] = value }); // assert Assert.AreEqual(value, translation.ToString(), "The translation returned an unexpected value given a valid base text."); @@ -303,19 +290,19 @@ namespace SMAPI.Tests.Core { ["default"] = new[] { - new Translation(string.Empty, "default", "key A", "default A"), - new Translation(string.Empty, "default", "key C", "default C") + new Translation("default", "key A", "default A"), + new Translation("default", "key C", "default C") }, ["en"] = new[] { - new Translation(string.Empty, "en", "key A", "en A"), - new Translation(string.Empty, "en", "key B", "en B"), - new Translation(string.Empty, "en", "key C", "default C") + new Translation("en", "key A", "en A"), + new Translation("en", "key B", "en B"), + new Translation("en", "key C", "default C") }, ["zzz"] = new[] { - new Translation(string.Empty, "zzz", "key A", "zzz A"), - new Translation(string.Empty, "zzz", "key C", "default C") + new Translation("zzz", "key A", "zzz A"), + new Translation("zzz", "key C", "default C") } }; expected["en-us"] = expected["en"].ToArray(); diff --git a/src/SMAPI/Framework/ModHelpers/TranslationHelper.cs b/src/SMAPI/Framework/ModHelpers/TranslationHelper.cs index 3252e047..65850384 100644 --- a/src/SMAPI/Framework/ModHelpers/TranslationHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/TranslationHelper.cs @@ -11,9 +11,6 @@ namespace StardewModdingAPI.Framework.ModHelpers /********* ** Fields *********/ - /// <summary>The name of the relevant mod for error messages.</summary> - private readonly string ModName; - /// <summary>The translations for each locale.</summary> private readonly IDictionary<string, IDictionary<string, string>> All = new Dictionary<string, IDictionary<string, string>>(StringComparer.InvariantCultureIgnoreCase); @@ -36,15 +33,11 @@ namespace StardewModdingAPI.Framework.ModHelpers *********/ /// <summary>Construct an instance.</summary> /// <param name="modID">The unique ID of the relevant mod.</param> - /// <param name="modName">The name of the relevant mod for error messages.</param> /// <param name="locale">The initial locale.</param> /// <param name="languageCode">The game's current language code.</param> - public TranslationHelper(string modID, string modName, string locale, LocalizedContentManager.LanguageCode languageCode) + public TranslationHelper(string modID, string locale, LocalizedContentManager.LanguageCode languageCode) : base(modID) { - // save data - this.ModName = modName; - // set locale this.SetLocale(locale, languageCode); } @@ -60,7 +53,7 @@ namespace StardewModdingAPI.Framework.ModHelpers public Translation Get(string key) { this.ForLocale.TryGetValue(key, out Translation translation); - return translation ?? new Translation(this.ModName, this.Locale, key, null); + return translation ?? new Translation(this.Locale, key, null); } /// <summary>Get a translation for the current locale.</summary> @@ -105,7 +98,7 @@ namespace StardewModdingAPI.Framework.ModHelpers foreach (var pair in translations) { if (!this.ForLocale.ContainsKey(pair.Key)) - this.ForLocale.Add(pair.Key, new Translation(this.ModName, this.Locale, pair.Key, pair.Value)); + this.ForLocale.Add(pair.Key, new Translation(this.Locale, pair.Key, pair.Value)); } } } diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index bc893abc..a4b38a50 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -960,7 +960,7 @@ namespace StardewModdingAPI.Framework IManifest manifest = mod.Manifest; IMonitor monitor = this.GetSecondaryMonitor(mod.DisplayName); IContentHelper contentHelper = new ContentHelper(this.ContentCore, mod.DirectoryPath, manifest.UniqueID, mod.DisplayName, monitor); - TranslationHelper translationHelper = new TranslationHelper(manifest.UniqueID, manifest.Name, contentCore.GetLocale(), contentCore.Language); + TranslationHelper translationHelper = new TranslationHelper(manifest.UniqueID, contentCore.GetLocale(), contentCore.Language); IContentPack contentPack = new ContentPack(mod.DirectoryPath, manifest, contentHelper, translationHelper, jsonHelper); mod.SetMod(contentPack, monitor, translationHelper); this.ModRegistry.Add(mod); @@ -1024,7 +1024,7 @@ namespace StardewModdingAPI.Framework // init mod helpers IMonitor monitor = this.GetSecondaryMonitor(mod.DisplayName); - TranslationHelper translationHelper = new TranslationHelper(manifest.UniqueID, manifest.Name, contentCore.GetLocale(), contentCore.Language); + TranslationHelper translationHelper = new TranslationHelper(manifest.UniqueID, contentCore.GetLocale(), contentCore.Language); IModHelper modHelper; { IModEvents events = new ModEvents(mod, this.EventManager); @@ -1040,7 +1040,7 @@ namespace StardewModdingAPI.Framework { IMonitor packMonitor = this.GetSecondaryMonitor(packManifest.Name); IContentHelper packContentHelper = new ContentHelper(contentCore, packDirPath, packManifest.UniqueID, packManifest.Name, packMonitor); - ITranslationHelper packTranslationHelper = new TranslationHelper(packManifest.UniqueID, packManifest.Name, contentCore.GetLocale(), contentCore.Language); + ITranslationHelper packTranslationHelper = new TranslationHelper(packManifest.UniqueID, contentCore.GetLocale(), contentCore.Language); return new ContentPack(packDirPath, packManifest, packContentHelper, packTranslationHelper, this.Toolkit.JsonHelper); } diff --git a/src/SMAPI/Translation.cs b/src/SMAPI/Translation.cs index e8698e2c..2196c8a5 100644 --- a/src/SMAPI/Translation.cs +++ b/src/SMAPI/Translation.cs @@ -15,9 +15,6 @@ namespace StardewModdingAPI /// <summary>The placeholder text when the translation is <c>null</c> or empty, where <c>{0}</c> is the translation key.</summary> internal const string PlaceholderText = "(no translation:{0})"; - /// <summary>The name of the relevant mod for error messages.</summary> - private readonly string ModName; - /// <summary>The locale for which the translation was fetched.</summary> private readonly string Locale; @@ -39,36 +36,11 @@ namespace StardewModdingAPI ** Public methods *********/ /// <summary>Construct an instance.</summary> - /// <param name="modName">The name of the relevant mod for error messages.</param> - /// <param name="locale">The locale for which the translation was fetched.</param> - /// <param name="key">The translation key.</param> - /// <param name="text">The underlying translation text.</param> - internal Translation(string modName, string locale, string key, string text) - : this(modName, locale, key, text, string.Format(Translation.PlaceholderText, key)) { } - - /// <summary>Construct an instance.</summary> - /// <param name="modName">The name of the relevant mod for error messages.</param> /// <param name="locale">The locale for which the translation was fetched.</param> /// <param name="key">The translation key.</param> /// <param name="text">The underlying translation text.</param> - /// <param name="placeholder">The value to return if the translations is undefined.</param> - internal Translation(string modName, string locale, string key, string text, string placeholder) - { - this.ModName = modName; - this.Locale = locale; - this.Key = key; - this.Text = text; - this.Placeholder = placeholder; - } - - /// <summary>Throw an exception if the translation text is <c>null</c> or empty.</summary> - /// <exception cref="KeyNotFoundException">There's no available translation matching the requested key and locale.</exception> - public Translation Assert() - { - if (!this.HasValue()) - throw new KeyNotFoundException($"The '{this.ModName}' mod doesn't have a translation with key '{this.Key}' for the '{this.Locale}' locale or its fallbacks."); - return this; - } + internal Translation(string locale, string key, string text) + : this(locale, key, text, string.Format(Translation.PlaceholderText, key)) { } /// <summary>Replace the text if it's <c>null</c> or empty. If you set a <c>null</c> or empty value, the translation will show the fallback "no translation" placeholder (see <see cref="UsePlaceholder"/> if you want to disable that). Returns a new instance if changed.</summary> /// <param name="default">The default value.</param> @@ -76,14 +48,14 @@ namespace StardewModdingAPI { return this.HasValue() ? this - : new Translation(this.ModName, this.Locale, this.Key, @default); + : new Translation(this.Locale, this.Key, @default); } /// <summary>Whether to return a "no translation" placeholder if the translation is <c>null</c> or empty. Returns a new instance.</summary> /// <param name="use">Whether to return a placeholder.</param> public Translation UsePlaceholder(bool use) { - return new Translation(this.ModName, this.Locale, this.Key, this.Text, use ? string.Format(Translation.PlaceholderText, this.Key) : null); + return new Translation(this.Locale, this.Key, this.Text, use ? string.Format(Translation.PlaceholderText, this.Key) : null); } /// <summary>Replace tokens in the text like <c>{{value}}</c> with the given values. Returns a new instance.</summary> @@ -127,7 +99,7 @@ namespace StardewModdingAPI ? value : match.Value; }); - return new Translation(this.ModName, this.Locale, this.Key, text); + return new Translation(this.Locale, this.Key, text); } /// <summary>Get whether the translation has a defined value.</summary> @@ -150,5 +122,22 @@ namespace StardewModdingAPI { return translation?.ToString(); } + + + /********* + ** Private methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="locale">The locale for which the translation was fetched.</param> + /// <param name="key">The translation key.</param> + /// <param name="text">The underlying translation text.</param> + /// <param name="placeholder">The value to return if the translations is undefined.</param> + private Translation(string locale, string key, string text, string placeholder) + { + this.Locale = locale; + this.Key = key; + this.Text = text; + this.Placeholder = placeholder; + } } } |