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
78
79
80
|
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
*********/
/// <inheritdoc />
public string Locale => this.Translator.Locale;
/// <inheritdoc />
public LocalizedContentManager.LanguageCode LocaleEnum => this.Translator.LocaleEnum;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="mod">The mod using this instance.</param>
/// <param name="locale">The initial locale.</param>
/// <param name="languageCode">The game's current language code.</param>
public TranslationHelper(IModMetadata mod, string locale, LocalizedContentManager.LanguageCode languageCode)
: base(mod)
{
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);
}
/// <inheritdoc />
public IDictionary<string, Translation> GetInAllLocales(string key, bool withFallback = false)
{
return this.Translator.GetInAllLocales(key, withFallback);
}
/// <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 pre-cache 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);
}
}
}
|