Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
LanguageManager.cs
Go to the documentation of this file.
1using System;
4using System.IO;
5using System.Linq;
9using CsvHelper;
10using Newtonsoft.Json;
16
17namespace Terraria.Localization;
18
19public class LanguageManager
20{
22
24
26
28
29 public GameCulture ActiveCulture { get; private set; }
30
32
34
36 {
38 }
39
40 public int GetCategorySize(string name)
41 {
43 {
45 }
46 return 0;
47 }
48
54
55 public void SetLanguage(string cultureName)
56 {
59 }
60
61 public int EstimateWordCount()
62 {
63 int num = 0;
64 foreach (string key in _localizedTexts.Keys)
65 {
66 string textValue = GetTextValue(key);
67 textValue.Replace(",", "").Replace(".", "").Replace("\"", "")
68 .Trim();
69 string[] array = textValue.Split(' ');
70 string[] array2 = textValue.Split(' ');
71 if (array.Length != array2.Length)
72 {
73 break;
74 }
75 string[] array3 = array;
76 foreach (string text in array3)
77 {
78 if (!string.IsNullOrWhiteSpace(text) && text.Length >= 1)
79 {
80 num++;
81 }
82 }
83 }
84 return num;
85 }
86
88 {
90 {
91 localizedText.Value.SetValue(localizedText.Key);
92 }
93 }
94
96 {
98 return Array.FindAll(typeof(Program).Assembly.GetManifestResourceNames(), (string element) => element.StartsWith("Terraria.Localization.Content." + culture.CultureInfo.Name) && element.EndsWith(".json"));
99 }
100
102 {
103 if (ActiveCulture != culture)
104 {
106 {
109 }
112 Thread.CurrentThread.CurrentCulture = culture.CultureInfo;
113 Thread.CurrentThread.CurrentUICulture = culture.CultureInfo;
114 if (this.OnLanguageChanged != null)
115 {
116 this.OnLanguageChanged(this);
117 }
119 }
120 }
121
123 {
125 if (this.OnLanguageChanging != null)
126 {
127 this.OnLanguageChanging(this);
128 }
130 {
132 }
134 }
135
137 {
139 foreach (string text in languageFilesForCulture)
140 {
141 try
142 {
144 if (text2 == null || text2.Length < 2)
145 {
146 throw new FormatException();
147 }
149 }
150 catch (Exception)
151 {
153 {
154 Debugger.Break();
155 }
156 Console.WriteLine("Failed to load language file: " + text);
157 break;
158 }
159 }
160 }
161
163 {
164 Regex regex = new Regex("{\\$(\\w+\\.\\w+)}", RegexOptions.Compiled);
166 {
168 for (int i = 0; i < 100; i++)
169 {
170 string text = regex.Replace(value.Value, (Match match) => GetTextValue(match.Groups[1].ToString()));
171 if (text == value.Value)
172 {
173 break;
174 }
175 value.SetValue(text);
176 }
177 }
178 }
179
181 {
182 string name = ActiveCulture.Name;
183 string text = ("Localization" + Path.DirectorySeparatorChar + name).ToLower();
186 {
187 foreach (string item2 in item.GetAllAssetsStartingWith(text))
188 {
189 string extension = item.GetExtension(item2);
190 if (!(extension == ".json") && !(extension == ".csv"))
191 {
192 continue;
193 }
194 using Stream stream = item.OpenStream(item2);
196 string fileText = streamReader.ReadToEnd();
197 if (extension == ".json")
198 {
200 }
201 if (extension == ".csv")
202 {
204 }
205 }
206 }
209 }
210
212 {
213 //IL_0008: Unknown result type (might be due to invalid IL or missing references)
214 //IL_000e: Expected O, but got Unknown
215 using TextReader textReader = new StringReader(fileText);
216 CsvReader val = new CsvReader(textReader);
217 try
218 {
219 val.Configuration.HasHeaderRecord = true;
220 if (!val.ReadHeader())
221 {
222 return;
223 }
224 string[] fieldHeaders = val.FieldHeaders;
225 int num = -1;
226 int num2 = -1;
227 for (int i = 0; i < fieldHeaders.Length; i++)
228 {
229 string text = fieldHeaders[i].ToLower();
230 if (text == "translation")
231 {
232 num2 = i;
233 }
234 if (text == "key")
235 {
236 num = i;
237 }
238 }
239 if (num == -1 || num2 == -1)
240 {
241 return;
242 }
243 int num3 = Math.Max(num, num2) + 1;
244 while (val.Read())
245 {
246 string[] currentRecord = val.CurrentRecord;
247 if (currentRecord.Length >= num3)
248 {
249 string text2 = currentRecord[num];
250 string value = currentRecord[num2];
251 if (!string.IsNullOrWhiteSpace(text2) && !string.IsNullOrWhiteSpace(value) && _localizedTexts.ContainsKey(text2))
252 {
253 _localizedTexts[text2].SetValue(value);
254 }
255 }
256 }
257 }
258 finally
259 {
260 ((IDisposable)val)?.Dispose();
261 }
262 }
263
265 {
267 {
268 _ = item.Key;
269 foreach (KeyValuePair<string, string> item2 in item.Value)
270 {
271 string key = item.Key + "." + item2.Key;
273 {
274 _localizedTexts[key].SetValue(item2.Value);
275 }
276 else if (canCreateCategories)
277 {
280 {
282 }
284 }
285 }
286 }
287 }
288
289 [Conditional("DEBUG")]
291 {
292 if (font == null)
293 {
294 return;
295 }
296 string text = "";
298 {
299 string value = value2.Value;
300 for (int i = 0; i < value.Length; i++)
301 {
302 char c = value[i];
303 if (!font.IsCharacterSupported(c))
304 {
305 text = text + value2.Key + ", " + c.ToString() + ", " + (int)c + "\n";
306 }
307 }
308 }
309 }
310
312 {
313 int num = 0;
315 {
316 if (regex.IsMatch(localizedText.Key))
317 {
318 num++;
319 }
320 }
321 LocalizedText[] array = new LocalizedText[num];
322 int num2 = 0;
324 {
325 if (regex.IsMatch(localizedText2.Key))
326 {
327 array[num2] = localizedText2.Value;
328 num2++;
329 }
330 }
331 return array;
332 }
333
346
348 {
349 int num = 0;
351 {
352 if (filter(localizedText.Key, localizedText.Value))
353 {
354 num++;
355 }
356 }
357 int num2 = (random ?? Main.rand).Next(num);
359 {
360 if (filter(localizedText2.Key, localizedText2.Value) && --num == num2)
361 {
362 return localizedText2.Value;
363 }
364 }
365 return LocalizedText.Empty;
366 }
367
369 {
371 {
372 return new LocalizedText(categoryName + ".RANDOM", categoryName + ".RANDOM");
373 }
375 return GetText(categoryName + "." + list[(random ?? Main.rand).Next(list.Count)]);
376 }
377
379 {
381 {
382 return new LocalizedText(categoryName + ".INDEXED", categoryName + ".INDEXED");
383 }
385 int index2 = index % list.Count;
386 return GetText(categoryName + "." + list[index2]);
387 }
388
389 public bool Exists(string key)
390 {
392 }
393
394 public LocalizedText GetText(string key)
395 {
397 {
398 return new LocalizedText(key, key);
399 }
400 return _localizedTexts[key];
401 }
402
403 public string GetTextValue(string key)
404 {
406 {
407 return _localizedTexts[key].Value;
408 }
409 return key;
410 }
411
412 public string GetTextValue(string key, object arg0)
413 {
415 {
416 return _localizedTexts[key].Format(arg0);
417 }
418 return key;
419 }
420
421 public string GetTextValue(string key, object arg0, object arg1)
422 {
424 {
425 return _localizedTexts[key].Format(arg0, arg1);
426 }
427 return key;
428 }
429
430 public string GetTextValue(string key, object arg0, object arg1, object arg2)
431 {
433 {
434 return _localizedTexts[key].Format(arg0, arg1, arg2);
435 }
436 return key;
437 }
438
439 public string GetTextValue(string key, params object[] args)
440 {
442 {
443 return _localizedTexts[key].Format(args);
444 }
445 return key;
446 }
447
452}
void Add(TKey key, TValue value)
static void WriteLine()
Definition Console.cs:733
static byte Max(byte val1, byte val2)
Definition Math.cs:738
virtual string[] GetManifestResourceNames()
Definition Assembly.cs:279
static RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMark)
Definition Assembly.cs:198
static Asset< DynamicSpriteFont > DeathText
Definition FontAssets.cs:12
static GameCulture FromLegacyId(int id)
static GameCulture FromName(string name)
readonly CultureInfo CultureInfo
static GameCulture DefaultCulture
void LoadLanguageFromFileTextJson(string fileText, bool canCreateCategories)
void SetLanguage(string cultureName)
string[] GetLanguageFilesForCulture(GameCulture culture)
string GetTextValue(string key, object arg0, object arg1, object arg2)
string GetTextValue(string key, object arg0, object arg1)
LanguageChangeCallback OnLanguageChanging
void LoadLanguageFromFileTextCsv(string fileText)
readonly Dictionary< string, List< string > > _categoryGroupedKeys
LocalizedText RandomFromCategory(string categoryName, UnifiedRandom random=null)
LanguageChangeCallback OnLanguageChanged
readonly Dictionary< string, LocalizedText > _localizedTexts
string GetTextValue(string key, object arg0)
string GetTextValue(string key, params object[] args)
void ValidateAllCharactersContainedInFont(DynamicSpriteFont font)
void LoadFilesForCulture(GameCulture culture)
void UseSources(List< IContentSource > sourcesFromLowestToHighest)
LocalizedText IndexedFromCategory(string categoryName, int index)
LocalizedText SelectRandom(LanguageSearchFilter filter, UnifiedRandom random=null)
void LoadLanguage(GameCulture culture, bool processCopyCommands=true)
LocalizedText[] FindAll(LanguageSearchFilter filter)
LocalizedText[] FindAll(Regex regex)
void SetLanguage(GameCulture culture)
void SetFallbackCulture(GameCulture culture)
static readonly LocalizedText Empty
static UnifiedRandom rand
Definition Main.cs:1387
static string ReadEmbeddedResource(string path)
Definition Utils.cs:1957
delegate bool LanguageSearchFilter(string key, LocalizedText text)
delegate void LanguageChangeCallback(LanguageManager languageManager)