-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Localization.cs
309 lines (283 loc) · 6.14 KB
/
Localization.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using System;
using System.Collections.Generic;
using UnityEngine;
public static class Localization
{
public delegate byte[] LoadFunction(string path);
public static LoadFunction loadFunction;
public static bool localizationHasBeenSet = false;
private static string[] mLanguages = null;
private static Dictionary<string, string> mOldDictionary = new Dictionary<string, string>();
private static Dictionary<string, string[]> mDictionary = new Dictionary<string, string[]>();
private static int mLanguageIndex = -1;
private static string mLanguage;
public static Dictionary<string, string[]> dictionary
{
get
{
if (!localizationHasBeenSet)
{
language = PlayerPrefs.GetString("Language", "English");
}
return mDictionary;
}
set
{
localizationHasBeenSet = (value != null);
mDictionary = value;
}
}
public static string[] knownLanguages
{
get
{
if (!localizationHasBeenSet)
{
LoadDictionary(PlayerPrefs.GetString("Language", "English"));
}
return mLanguages;
}
}
public static string language
{
get
{
if (string.IsNullOrEmpty(mLanguage))
{
string[] knownLanguages = Localization.knownLanguages;
mLanguage = PlayerPrefs.GetString("Language", (knownLanguages == null) ? "English" : knownLanguages[0]);
LoadAndSelect(mLanguage);
}
return mLanguage;
}
set
{
if (mLanguage != value)
{
mLanguage = value;
LoadAndSelect(value);
}
}
}
[Obsolete("Localization is now always active. You no longer need to check this property.")]
public static bool isActive
{
get
{
return true;
}
}
private static bool LoadDictionary(string value)
{
byte[] array = null;
if (!localizationHasBeenSet)
{
if (loadFunction == null)
{
TextAsset textAsset = Resources.Load<TextAsset>("Localization");
if (textAsset != null)
{
array = textAsset.bytes;
}
}
else
{
array = loadFunction("Localization");
}
localizationHasBeenSet = true;
}
if (LoadCSV(array))
{
return true;
}
if (string.IsNullOrEmpty(value))
{
return false;
}
if (loadFunction == null)
{
TextAsset textAsset2 = Resources.Load<TextAsset>(value);
if (textAsset2 != null)
{
array = textAsset2.bytes;
}
}
else
{
array = loadFunction(value);
}
if (array != null)
{
Set(value, array);
return true;
}
return false;
}
private static bool LoadAndSelect(string value)
{
if (!string.IsNullOrEmpty(value))
{
if (mDictionary.Count == 0 && !LoadDictionary(value))
{
return false;
}
if (SelectLanguage(value))
{
return true;
}
}
if (mOldDictionary.Count > 0)
{
return true;
}
mOldDictionary.Clear();
mDictionary.Clear();
if (string.IsNullOrEmpty(value))
{
PlayerPrefs.DeleteKey("Language");
}
return false;
}
public static void Load(TextAsset asset)
{
ByteReader byteReader = new ByteReader(asset);
Set(asset.name, byteReader.ReadDictionary());
}
public static void Set(string languageName, byte[] bytes)
{
ByteReader byteReader = new ByteReader(bytes);
Set(languageName, byteReader.ReadDictionary());
}
public static bool LoadCSV(TextAsset asset)
{
return LoadCSV(asset.bytes, asset);
}
public static bool LoadCSV(byte[] bytes)
{
return LoadCSV(bytes, null);
}
private static bool LoadCSV(byte[] bytes, TextAsset asset)
{
if (bytes == null)
{
return false;
}
ByteReader byteReader = new ByteReader(bytes);
BetterList<string> betterList = byteReader.ReadCSV();
if (betterList.size < 2)
{
return false;
}
betterList[0] = "KEY";
if (!string.Equals(betterList[0], "KEY"))
{
Debug.LogError("Invalid localization CSV file. The first value is expected to be 'KEY', followed by language columns.\nInstead found '" + betterList[0] + "'", asset);
return false;
}
mLanguages = new string[betterList.size - 1];
for (int i = 0; i < mLanguages.Length; i++)
{
mLanguages[i] = betterList[i + 1];
}
mDictionary.Clear();
while (betterList != null)
{
AddCSV(betterList);
betterList = byteReader.ReadCSV();
}
return true;
}
private static bool SelectLanguage(string language)
{
mLanguageIndex = -1;
if (mDictionary.Count == 0)
{
return false;
}
if (mDictionary.TryGetValue("KEY", out string[] value))
{
for (int i = 0; i < value.Length; i++)
{
if (value[i] == language)
{
mOldDictionary.Clear();
mLanguageIndex = i;
mLanguage = language;
PlayerPrefs.SetString("Language", mLanguage);
UIRoot.Broadcast("OnLocalize");
return true;
}
}
}
return false;
}
private static void AddCSV(BetterList<string> values)
{
if (values.size >= 2)
{
string[] array = new string[values.size - 1];
for (int i = 1; i < values.size; i++)
{
array[i - 1] = values[i];
}
try
{
mDictionary.Add(values[0], array);
}
catch (Exception ex)
{
Debug.LogError("Unable to add '" + values[0] + "' to the Localization dictionary.\n" + ex.Message);
}
}
}
public static void Set(string languageName, Dictionary<string, string> dictionary)
{
mLanguage = languageName;
PlayerPrefs.SetString("Language", mLanguage);
mOldDictionary = dictionary;
localizationHasBeenSet = false;
mLanguageIndex = -1;
mLanguages = new string[1]
{
languageName
};
UIRoot.Broadcast("OnLocalize");
}
public static string Get(string key)
{
if (!localizationHasBeenSet)
{
language = PlayerPrefs.GetString("Language", "English");
}
string value2;
if (mLanguageIndex != -1 && mDictionary.TryGetValue(key, out string[] value))
{
if (mLanguageIndex < value.Length)
{
return value[mLanguageIndex];
}
}
else if (mOldDictionary.TryGetValue(key, out value2))
{
return value2;
}
return key;
}
public static string Format(string key, params object[] parameters)
{
return string.Format(Get(key), parameters);
}
[Obsolete("Use Localization.Get instead")]
public static string Localize(string key)
{
return Get(key);
}
public static bool Exists(string key)
{
if (!localizationHasBeenSet)
{
language = PlayerPrefs.GetString("Language", "English");
}
return mDictionary.ContainsKey(key) || mOldDictionary.ContainsKey(key);
}
}