using System.Collections.Generic; using StardewValley; namespace StardewModdingAPI.Framework.ModHelpers { /// Provides translations stored in the mod's i18n folder, with one file per locale (like en.json) containing a flat key => value structure. Translations are fetched with locale fallback, so missing translations are filled in from broader locales (like pt-BR.json < pt.json < default.json). internal class TranslationHelper : BaseHelper, ITranslationHelper { /********* ** Fields *********/ /// The underlying translation manager. private readonly Translator Translator; /********* ** Accessors *********/ /// The current locale. public string Locale => this.Translator.Locale; /// The game's current language code. public LocalizedContentManager.LanguageCode LocaleEnum => this.Translator.LocaleEnum; /********* ** Public methods *********/ /// Construct an instance. /// The unique ID of the relevant mod. /// The initial locale. /// The game's current language code. public TranslationHelper(string modID, string locale, LocalizedContentManager.LanguageCode languageCode) : base(modID) { this.Translator = new Translator(); this.Translator.SetLocale(locale, languageCode); } /// Get all translations for the current locale. public IEnumerable GetTranslations() { return this.Translator.GetTranslations(); } /// Get a translation for the current locale. /// The translation key. public Translation Get(string key) { return this.Translator.Get(key); } /// Get a translation for the current locale. /// The translation key. /// An object containing token key/value pairs. This can be an anonymous object (like new { value = 42, name = "Cranberries" }), a dictionary, or a class instance. public Translation Get(string key, object tokens) { return this.Translator.Get(key, tokens); } /// Set the translations to use. /// The translations to use. internal TranslationHelper SetTranslations(IDictionary> translations) { this.Translator.SetTranslations(translations); return this; } /// Set the current locale and precache translations. /// The current locale. /// The game's current language code. internal void SetLocale(string locale, LocalizedContentManager.LanguageCode localeEnum) { this.Translator.SetLocale(locale, localeEnum); } } }