-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathTranslationCache.cs
More file actions
60 lines (54 loc) · 2.17 KB
/
TranslationCache.cs
File metadata and controls
60 lines (54 loc) · 2.17 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RT.Json;
using RT.Serialization;
using RT.Util;
using RT.Util.ExtensionMethods;
namespace KtaneWeb
{
public sealed partial class KtanePropellerModule
{
private Dictionary<string, TranslationInfo> _translationCache;
// This method is called in Init() (when the server is initialized) and in pull() (when the repo is updated due to a new git commit).
private void generateTranslationCache()
{
var path = Path.Combine(_config.BaseDir, "Translations");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
#if DEBUG
ClassifyJson.SerializeToFile(TranslationInfo.Default, Path.Combine(path, "en.json"));
#endif
_translationCache = new DirectoryInfo(path)
.EnumerateFiles("*.json", SearchOption.TopDirectoryOnly)
.ParallelSelect(Environment.ProcessorCount, file =>
{
try
{
var translationJson = File.ReadAllText(file.FullName);
var translation = ClassifyJson.Deserialize<TranslationInfo>(JsonDict.Parse(translationJson));
translation.langCode = file.Name.Remove(file.Name.Length - 5);
var newJson = ClassifyJson.Serialize(translation);
translation.Json = newJson.ToString();
#if DEBUG
var newJsonIndented = newJson.ToStringIndented();
if (translationJson != newJsonIndented)
File.WriteAllText(file.FullName, newJsonIndented);
#endif
return translation;
}
catch (Exception e)
{
#if DEBUG
Console.WriteLine(e.Message);
Console.WriteLine(e.GetType().FullName);
Console.WriteLine(e.StackTrace);
#endif
Log.Exception(e);
return null;
}
}).ToDictionary(t => t.langCode, t => t);
}
}
}