summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-05-28 01:49:21 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-05-28 01:49:21 -0400
commit5af58c7b18a120ce47f230ede7f116678d97e038 (patch)
tree0639add41b5e6b9e234d8e2c17884fd0153c76b9
parent12ffd9c334906e2256a1c32976b6a07f75027f25 (diff)
downloadSMAPI-5af58c7b18a120ce47f230ede7f116678d97e038.tar.gz
SMAPI-5af58c7b18a120ce47f230ede7f116678d97e038.tar.bz2
SMAPI-5af58c7b18a120ce47f230ede7f116678d97e038.zip
refactor translation init for reuse (#296)
-rw-r--r--src/StardewModdingAPI.Tests/TranslationTests.cs6
-rw-r--r--src/StardewModdingAPI/Framework/ModHelper.cs5
-rw-r--r--src/StardewModdingAPI/Framework/TranslationHelper.cs20
-rw-r--r--src/StardewModdingAPI/Program.cs63
4 files changed, 56 insertions, 38 deletions
diff --git a/src/StardewModdingAPI.Tests/TranslationTests.cs b/src/StardewModdingAPI.Tests/TranslationTests.cs
index 3e6e6499..76da8706 100644
--- a/src/StardewModdingAPI.Tests/TranslationTests.cs
+++ b/src/StardewModdingAPI.Tests/TranslationTests.cs
@@ -40,7 +40,7 @@ namespace StardewModdingAPI.Tests
var data = new Dictionary<string, IDictionary<string, string>>();
// act
- ITranslationHelper helper = new TranslationHelper("ModName", "en", LocalizedContentManager.LanguageCode.en, data);
+ ITranslationHelper helper = new TranslationHelper("ModName", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
Translation translation = helper.Translate("key");
// assert
@@ -62,7 +62,7 @@ namespace StardewModdingAPI.Tests
// act
var actual = new Dictionary<string, IDictionary<string, string>>();
- TranslationHelper helper = new TranslationHelper("ModName", "en", LocalizedContentManager.LanguageCode.en, data);
+ TranslationHelper helper = new TranslationHelper("ModName", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
foreach (string locale in expected.Keys)
{
this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en);
@@ -86,7 +86,7 @@ namespace StardewModdingAPI.Tests
// act
var actual = new Dictionary<string, IDictionary<string, string>>();
- TranslationHelper helper = new TranslationHelper("ModName", "en", LocalizedContentManager.LanguageCode.en, data);
+ TranslationHelper helper = new TranslationHelper("ModName", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
foreach (string locale in expected.Keys)
{
this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en);
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
/// <param name="commandManager">Manages console commands.</param>
/// <param name="contentManager">The content manager which loads content assets.</param>
/// <param name="reflection">Simplifies access to private game code.</param>
- /// <param name="translations">Provides translations stored in the mod folder.</param>
/// <exception cref="ArgumentNullException">An argument is null or empty.</exception>
/// <exception cref="InvalidOperationException">The <paramref name="modDirectory"/> path does not exist on disk.</exception>
- 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
/// <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>
- /// <param name="translations">The translations for each locale.</param>
- public TranslationHelper(string modName, string locale, LocalizedContentManager.LanguageCode languageCode, IDictionary<string, IDictionary<string, string>> 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<string, string>(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);
}
+ /// <summary>Set the translations to use.</summary>
+ /// <param name="translations">The translations to use.</param>
+ internal TranslationHelper SetTranslations(IDictionary<string, IDictionary<string, string>> translations)
+ {
+ // reset translations
+ this.All.Clear();
+ foreach (var pair in translations)
+ this.All[pair.Key] = new Dictionary<string, string>(pair.Value, StringComparer.InvariantCultureIgnoreCase);
+
+ // rebuild cache
+ this.SetLocale(this.Locale, this.LocaleEnum);
+
+ return this;
+ }
+
/// <summary>Set the current locale and precache translations.</summary>
/// <param name="locale">The current locale.</param>
/// <param name="localeEnum">The game's current language code.</param>
diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs
index 47ba7c30..6939a03d 100644
--- a/src/StardewModdingAPI/Program.cs
+++ b/src/StardewModdingAPI/Program.cs
@@ -640,36 +640,9 @@ namespace StardewModdingAPI
continue;
}
- // get translations
- TranslationHelper translations;
- {
- IDictionary<string, IDictionary<string, string>> translationValues = new Dictionary<string, IDictionary<string, string>>();
-
- // read translation files
- DirectoryInfo translationsDir = new DirectoryInfo(Path.Combine(metadata.DirectoryPath, "i18n"));
- if (translationsDir.Exists)
- {
- foreach (FileInfo file in translationsDir.EnumerateFiles("*.json"))
- {
- string locale = Path.GetFileNameWithoutExtension(file.Name.ToLower().Trim());
- try
- {
- translationValues[locale] = jsonHelper.ReadJsonFile<IDictionary<string, string>>(file.FullName);
- }
- catch (Exception ex)
- {
- this.Monitor.Log($"Couldn't read {metadata.DisplayName}'s i18n/{locale}.json file: {ex.GetLogSummary()}");
- }
- }
- }
-
- // create translation helper
- translations = new TranslationHelper(metadata.DisplayName, contentManager.GetLocale(), contentManager.GetCurrentLanguage(), translationValues);
- }
-
// inject data
mod.ModManifest = manifest;
- mod.Helper = new ModHelper(metadata.DisplayName, metadata.DirectoryPath, jsonHelper, this.ModRegistry, this.CommandManager, contentManager, this.Reflection, translations);
+ mod.Helper = new ModHelper(metadata.DisplayName, metadata.DirectoryPath, jsonHelper, this.ModRegistry, this.CommandManager, contentManager, this.Reflection);
mod.Monitor = this.GetSecondaryMonitor(metadata.DisplayName);
mod.PathOnDisk = metadata.DirectoryPath;
@@ -685,6 +658,9 @@ namespace StardewModdingAPI
}
}
+ // initialise translations
+ this.ReloadTranslations();
+
// initialise loaded mods
foreach (IModMetadata metadata in this.ModRegistry.GetMods())
{
@@ -711,6 +687,37 @@ namespace StardewModdingAPI
return modsLoaded;
}
+ /// <summary>Reload translations for all mods.</summary>
+ private void ReloadTranslations()
+ {
+ JsonHelper jsonHelper = new JsonHelper();
+ foreach (IModMetadata metadata in this.ModRegistry.GetMods())
+ {
+ // read translation files
+ IDictionary<string, IDictionary<string, string>> translations = new Dictionary<string, IDictionary<string, string>>();
+ DirectoryInfo translationsDir = new DirectoryInfo(Path.Combine(metadata.DirectoryPath, "i18n"));
+ if (translationsDir.Exists)
+ {
+ foreach (FileInfo file in translationsDir.EnumerateFiles("*.json"))
+ {
+ string locale = Path.GetFileNameWithoutExtension(file.Name.ToLower().Trim());
+ try
+ {
+ translations[locale] = jsonHelper.ReadJsonFile<IDictionary<string, string>>(file.FullName);
+ }
+ catch (Exception ex)
+ {
+ this.Monitor.Log($"Couldn't read {metadata.DisplayName}'s i18n/{locale}.json file: {ex.GetLogSummary()}");
+ }
+ }
+ }
+
+ // update translation
+ TranslationHelper translationHelper = (TranslationHelper)metadata.Mod.Helper.Translation;
+ translationHelper.SetTranslations(translations);
+ }
+ }
+
/// <summary>The method called when the user submits the help command in the console.</summary>
/// <param name="name">The command name.</param>
/// <param name="arguments">The command arguments.</param>