-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShopLocalisation.cs
140 lines (132 loc) · 5.31 KB
/
ShopLocalisation.cs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using UnityEngine;
using UnityEngine.Localization.Settings;
namespace ContentWarningShop.Localisation
{
/// <summary>
/// Contains the locale identifiers that are supported by the game and guaranteed to resolve.
/// </summary>
public static class LocaleKeys
{
public const string English = "en";
public const string Swedish = "sv";
public const string French = "fr";
public const string German = "de";
public const string Italian = "it";
public const string Portuguese = "pt-br";
public const string Spanish = "es";
public const string Ukrainian = "uk";
public const string ChineseSimplified = "zh-hans";
public const string ChineseTraditional = "zh-hant";
public const string Japanese = "ja";
public const string Korean = "ko";
public const string Russian = "ru";
}
public static class ShopLocalisation
{
private static readonly Dictionary<UnityEngine.Localization.Locale, Dictionary<string, string>> _localeStrings = new();
public static readonly string TooltipsSuffix = "_ToolTips";
/// <summary>
/// Represents the Left Mouse Button glyph.
/// </summary>
public static readonly string UseGlyph = "{key_use}";
/// <summary>
/// Represents the Right Mouse Button glyph.
/// </summary>
public static readonly string Use2Glyph = "{key_use2}";
/// <summary>
/// Represents the R key glyph (default, can be rebound by the player).
/// </summary>
public static readonly string SelfieGlyph = "{key_selfie}";
/// <summary>
/// Represents the Mouse Wheel glyph.
/// </summary>
public static readonly string ZoomGlyph = "{key_zoom}";
static ShopLocalisation()
{
foreach (var loc in LocalizationSettings.AvailableLocales.Locales)
{
if (_localeStrings.ContainsKey(loc) == false)
{
_localeStrings.Add(loc, new Dictionary<string, string>());
}
}
Debug.Log($"ShopLocalisation loaded {_localeStrings.Count} locales.");
}
/// <summary>
/// Gets the currently used locale.
/// </summary>
/// <returns></returns>
public static UnityEngine.Localization.Locale GetCurrentLocale()
{
return LocalizationSettings.SelectedLocale;
}
/// <summary>
/// Gets a locale with the specified name. See <see cref="LocaleKeys"/> for locale keys that are guaranteed to exist.
/// </summary>
/// <param name="locId"></param>
/// <param name="locale"></param>
/// <returns><see langword="true"/> if a locale with the given name was found; otherwise <see langword="false"/>.</returns>
public static bool TryGetLocale(string locId, out UnityEngine.Localization.Locale locale)
{
UnityEngine.Localization.Locale loc = null;
foreach (var sLoc in _localeStrings.Keys)
{
if (sLoc.LocaleName.ToLower().Contains(locId))
{
loc = sLoc;
break;
}
}
locale = loc;
return loc != null;
}
/// <summary>
/// Registers a localised string with the given key to the selected locale.
/// </summary>
/// <param name="loc"></param>
/// <param name="key"></param>
/// <param name="str"></param>
public static void AddLocaleString(this UnityEngine.Localization.Locale loc, string key, string str)
{
if (_localeStrings.ContainsKey(loc) == false)
{
return;
}
if (_localeStrings[loc].ContainsKey(key) == false)
{
_localeStrings[loc].Add(key, str);
}
else
{
_localeStrings[loc][key] = str;
}
}
/// <summary>
/// Gets the localised string associated with the given key based on the current locale.
/// </summary>
/// <param name="key"></param>
/// <param name="res"></param>
/// <returns><see langword="true"/> if the key was found; otherwise <see langword="false"/>.</returns>
public static bool TryGetLocaleString(string key, out string res)
{
var currLoc = GetCurrentLocale();
var found = _localeStrings[currLoc].TryGetValue(key, out string str);
res = str;
return found;
}
/// <summary>
/// Sets default tooltip string on the item. This will be used if you don't otherwise provide localisation for your item.
/// </summary>
/// <param name="item"></param>
/// <param name="tooltipString">A ";" separated string of item tooltips. See <see href="https://github.com/Xerren09/ContentWarningShopAPI#localisation"/> for more.</param>
public static void SetDefaultTooltips(this Item item, string tooltipString)
{
var tooltips = tooltipString.Split(';');
item.Tooltips = new();
foreach (var tooltip in tooltips)
{
item.Tooltips.Add(new ItemKeyTooltip(tooltip, null, null));
}
}
}
}