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
75
76
77
|
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> < <c>pt.json</c> < <c>default.json</c>).</summary>
internal class TranslationHelper : BaseHelper, ITranslationHelper
{
/*********
** Fields
*********/
/// <summary>The underlying translation manager.</summary>
private readonly Translator Translator;
/*********
** Accessors
*********/
/// <summary>The current locale.</summary>
public string Locale => this.Translator.Locale;
/// <summary>The game's current language code.</summary>
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);
}
/// <summary>Get all translations for the current locale.</summary>
public IEnumerable<Translation> GetTranslations()
{
return this.Translator.GetTranslations();
}
/// <summary>Get a translation for the current locale.</summary>
/// <param name="key">The translation key.</param>
public Translation Get(string key)
{
return this.Translator.Get(key);
}
/// <summary>Get a translation for the current locale.</summary>
/// <param name="key">The translation key.</param>
/// <param name="tokens">An object containing token key/value pairs. This can be an anonymous object (like <c>new { value = 42, name = "Cranberries" }</c>), a dictionary, or a class instance.</param>
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);
}
}
}
|