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
*********/
///
public string Locale => this.Translator.Locale;
///
public LocalizedContentManager.LanguageCode LocaleEnum => this.Translator.LocaleEnum;
/*********
** Public methods
*********/
/// Construct an instance.
/// The mod using this instance.
/// The initial locale.
/// The game's current language code.
public TranslationHelper(IModMetadata mod, string locale, LocalizedContentManager.LanguageCode languageCode)
: base(mod)
{
this.Translator = new Translator();
this.Translator.SetLocale(locale, languageCode);
}
///
public IEnumerable GetTranslations()
{
return this.Translator.GetTranslations();
}
///
public Translation Get(string key)
{
return this.Translator.Get(key);
}
///
public Translation Get(string key, object? tokens)
{
return this.Translator.Get(key, tokens);
}
///
public IDictionary GetInAllLocales(string key, bool withFallback = false)
{
return this.Translator.GetInAllLocales(key, withFallback);
}
/// 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 pre-cache translations.
/// The current locale.
/// The game's current language code.
internal void SetLocale(string locale, LocalizedContentManager.LanguageCode localeEnum)
{
this.Translator.SetLocale(locale, localeEnum);
}
}
}