From 5af58c7b18a120ce47f230ede7f116678d97e038 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 28 May 2017 01:49:21 -0400 Subject: refactor translation init for reuse (#296) --- src/StardewModdingAPI/Framework/ModHelper.cs | 5 ++--- src/StardewModdingAPI/Framework/TranslationHelper.cs | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'src/StardewModdingAPI/Framework') diff --git a/src/StardewModdingAPI/Framework/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelper.cs index 8c578dbe..947b1ae8 100644 --- a/src/StardewModdingAPI/Framework/ModHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelper.cs @@ -47,10 +47,9 @@ namespace StardewModdingAPI.Framework /// Manages console commands. /// The content manager which loads content assets. /// Simplifies access to private game code. - /// Provides translations stored in the mod folder. /// An argument is null or empty. /// The path does not exist on disk. - public ModHelper(string displayName, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager, SContentManager contentManager, IReflectionHelper reflection, ITranslationHelper translations) + public ModHelper(string displayName, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager, SContentManager contentManager, IReflectionHelper reflection) { // validate if (string.IsNullOrWhiteSpace(modDirectory)) @@ -69,7 +68,7 @@ namespace StardewModdingAPI.Framework this.ModRegistry = modRegistry; this.ConsoleCommands = new CommandHelper(displayName, commandManager); this.Reflection = reflection; - this.Translation = translations; + this.Translation = new TranslationHelper(displayName, contentManager.GetLocale(), contentManager.GetCurrentLanguage()); } /**** diff --git a/src/StardewModdingAPI/Framework/TranslationHelper.cs b/src/StardewModdingAPI/Framework/TranslationHelper.cs index c3104d1b..ebcd69b1 100644 --- a/src/StardewModdingAPI/Framework/TranslationHelper.cs +++ b/src/StardewModdingAPI/Framework/TranslationHelper.cs @@ -41,13 +41,10 @@ namespace StardewModdingAPI.Framework /// The name of the relevant mod for error messages. /// The initial locale. /// The game's current language code. - /// The translations for each locale. - public TranslationHelper(string modName, string locale, LocalizedContentManager.LanguageCode languageCode, IDictionary> translations) + public TranslationHelper(string modName, string locale, LocalizedContentManager.LanguageCode languageCode) { // save data this.ModName = modName; - foreach (var pair in translations) - this.All[pair.Key] = new Dictionary(pair.Value, StringComparer.InvariantCultureIgnoreCase); // set locale this.SetLocale(locale, languageCode); @@ -67,6 +64,21 @@ namespace StardewModdingAPI.Framework return new Translation(this.ModName, this.Locale, key, text); } + /// Set the translations to use. + /// The translations to use. + internal TranslationHelper SetTranslations(IDictionary> translations) + { + // reset translations + this.All.Clear(); + foreach (var pair in translations) + this.All[pair.Key] = new Dictionary(pair.Value, StringComparer.InvariantCultureIgnoreCase); + + // rebuild cache + this.SetLocale(this.Locale, this.LocaleEnum); + + return this; + } + /// Set the current locale and precache translations. /// The current locale. /// The game's current language code. -- cgit