summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/TranslationHelper.cs
blob: a88ca9c90accd8127e3cde41a65922c5b4c5f633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.Collections.Generic;
using StardewValley;

namespace StardewModdingAPI.Framework.ModHelpers
{
    /// <summary>Provides translations stored in the mod's <c>i18n</c> folder, with one file per locale (like <c>en.json</c>) containing a flat key => value structure. Translations are fetched with locale fallback, so missing translations are filled in from broader locales (like <c>pt-BR.json</c> &lt; <c>pt.json</c> &lt; <c>default.json</c>).</summary>
    internal class TranslationHelper : BaseHelper, ITranslationHelper
    {
        /*********
        ** Fields
        *********/
        /// <summary>The underlying translation manager.</summary>
        private readonly Translator Translator;


        /*********
        ** Accessors
        *********/
        /// <inheritdoc />
        public string Locale => this.Translator.Locale;

        /// <inheritdoc />
        public LocalizedContentManager.LanguageCode LocaleEnum => this.Translator.LocaleEnum;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="modID">The unique ID of the relevant mod.</param>
        /// <param name="locale">The initial locale.</param>
        /// <param name="languageCode">The game's current language code.</param>
        public TranslationHelper(string modID, string locale, LocalizedContentManager.LanguageCode languageCode)
            : base(modID)
        {
            this.Translator = new Translator();
            this.Translator.SetLocale(locale, languageCode);
        }

        /// <inheritdoc />
        public IEnumerable<Translation> GetTranslations()
        {
            return this.Translator.GetTranslations();
        }

        /// <inheritdoc />
        public Translation Get(string key)
        {
            return this.Translator.Get(key);
        }

        /// <inheritdoc />
        public Translation Get(string key, object tokens)
        {
            return this.Translator.Get(key, 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)
        {
            this.Translator.SetTranslations(translations);
            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>
        internal void SetLocale(string locale, LocalizedContentManager.LanguageCode localeEnum)
        {
            this.Translator.SetLocale(locale, localeEnum);
        }
    }
}