diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-05-28 17:53:44 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-05-28 17:53:44 -0400 |
commit | 90275f1d56a6d74f93021d9b15a742f381ec4bfd (patch) | |
tree | 57fe089cd0cfe9cfa3c254249d23dfd653735112 /src/StardewModdingAPI | |
parent | e20db6e8e4279dc95371e6f98c0e73dd19020c0f (diff) | |
download | SMAPI-90275f1d56a6d74f93021d9b15a742f381ec4bfd.tar.gz SMAPI-90275f1d56a6d74f93021d9b15a742f381ec4bfd.tar.bz2 SMAPI-90275f1d56a6d74f93021d9b15a742f381ec4bfd.zip |
add shortcut for translation with tokens (#296)
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r-- | src/StardewModdingAPI/Framework/TranslationHelper.cs | 8 | ||||
-rw-r--r-- | src/StardewModdingAPI/ITranslationHelper.cs | 5 | ||||
-rw-r--r-- | src/StardewModdingAPI/Translation.cs | 4 |
3 files changed, 15 insertions, 2 deletions
diff --git a/src/StardewModdingAPI/Framework/TranslationHelper.cs b/src/StardewModdingAPI/Framework/TranslationHelper.cs index e4a178e2..1e73c425 100644 --- a/src/StardewModdingAPI/Framework/TranslationHelper.cs +++ b/src/StardewModdingAPI/Framework/TranslationHelper.cs @@ -60,6 +60,14 @@ namespace StardewModdingAPI.Framework return new Translation(this.ModName, this.Locale, key, text); } + /// <summary>Get a translation for the current locale.</summary> + /// <param name="key">The translation key.</param> + /// <param name="tokens">An object containing token key/value pairs. This can be an anonymous object (like <c>new { value = 42, name = "Cranberries" }</c>), a dictionary, or a class instance.</param> + public Translation Get(string key, object tokens) + { + return this.Get(key).Tokens(tokens); + } + /// <summary>Set the translations to use.</summary> /// <param name="translations">The translations to use.</param> internal TranslationHelper SetTranslations(IDictionary<string, IDictionary<string, string>> translations) diff --git a/src/StardewModdingAPI/ITranslationHelper.cs b/src/StardewModdingAPI/ITranslationHelper.cs index 23980a1b..15f6b3c8 100644 --- a/src/StardewModdingAPI/ITranslationHelper.cs +++ b/src/StardewModdingAPI/ITranslationHelper.cs @@ -25,5 +25,10 @@ namespace StardewModdingAPI /// <summary>Get a translation for the current locale.</summary> /// <param name="key">The translation key.</param> Translation Get(string key); + + /// <summary>Get a translation for the current locale.</summary> + /// <param name="key">The translation key.</param> + /// <param name="tokens">An object containing token key/value pairs. This can be an anonymous object (like <c>new { value = 42, name = "Cranberries" }</c>), a dictionary, or a class instance.</param> + Translation Get(string key, object tokens); } } diff --git a/src/StardewModdingAPI/Translation.cs b/src/StardewModdingAPI/Translation.cs index 31a1b7e1..b86efc10 100644 --- a/src/StardewModdingAPI/Translation.cs +++ b/src/StardewModdingAPI/Translation.cs @@ -83,12 +83,12 @@ namespace StardewModdingAPI } /// <summary>Replace tokens in the text like <c>{{value}}</c> with the given values. Returns a new instance.</summary> - /// <param name="tokens">An object containing token key/value pairs. This can be an anonymous object (like <c>new { value = 42, name = "Cranberries" }</c>) or a dictionary of token values.</param> + /// <param name="tokens">An object containing token key/value pairs. This can be an anonymous object (like <c>new { value = 42, name = "Cranberries" }</c>), a dictionary, or a class instance.</param> /// <exception cref="ArgumentNullException">The <paramref name="tokens"/> argument is <c>null</c>.</exception> public Translation Tokens(object tokens) { if (tokens == null) - throw new ArgumentNullException(nameof(tokens)); + return this; // get dictionary of tokens IDictionary<string, string> tokenLookup = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase); |