-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLocalizationService.cs
179 lines (161 loc) · 6.14 KB
/
LocalizationService.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
using Stunlock.Core;
using System.Reflection;
using System.Text.Json;
namespace Eclipse;
internal class LocalizationService
{
struct Code
{
public string Key { get; set; }
public string Value { get; set; }
public string Description { get; set; }
}
struct Node
{
public string Guid { get; set; }
public string Text { get; set; }
}
struct Words
{
public string Original { get; set; }
public string Translation { get; set; }
}
struct LocalizationFile
{
public Code[] Codes { get; set; }
public Node[] Nodes { get; set; }
public Words[] Words { get; set; }
}
/*
static readonly string _language = ConfigService.LanguageLocalization;
static readonly Dictionary<string, string> _localizedLanguages = new()
{
{"English", "Bloodcraft.Localization.English.json"},
{"German", "Bloodcraft.Localization.German.json"},
{"French", "Bloodcraft.Localization.French.json"},
{"Spanish", "Bloodcraft.Localization.Spanish.json"},
{"Italian", "Bloodcraft.Localization.Italian.json"},
{"Japanese", "Bloodcraft.Localization.Japanese.json"},
{"Koreana", "Bloodcraft.Localization.Koreana.json"},
{"Portuguese", "Bloodcraft.Localization.Portuguese.json"},
{"Russian", "Bloodcraft.Localization.Russian.json"},
{"SimplifiedChinese", "Bloodcraft.Localization.SChinese.json"},
{"TraditionalChinese", "Bloodcraft.Localization.TChinese.json"},
{"Hungarian", "Bloodcraft.Localization.Hungarian.json"},
{"Latam", "Bloodcraft.Localization.Latam.json"},
{"Polish", "Bloodcraft.Localization.Polish.json"},
{"Thai", "Bloodcraft.Localization.Thai.json"},
{"Turkish", "Bloodcraft.Localization.Turkish.json"},
{"Vietnamese", "Bloodcraft.Localization.Vietnamese.json"},
{"Brazilian", "Bloodcraft.Localization.Brazilian.json"}
};
*/
static readonly Dictionary<int, string> _prefabHashesToGuidStrings = [];
static readonly Dictionary<string, string> _guidStringsToLocalizedNames = [];
public LocalizationService()
{
InitializeLocalizations();
}
static void InitializeLocalizations()
{
LoadPrefabHashesToGuidStrings();
LoadGuidStringsToLocalizedNames();
}
static void LoadPrefabHashesToGuidStrings()
{
string resourceName = "Eclipse.Localization.Prefabs.json";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(resourceName);
using StreamReader reader = new(stream);
string jsonContent = reader.ReadToEnd();
var prefabNames = JsonSerializer.Deserialize<Dictionary<int, string>>(jsonContent);
prefabNames
.ForEach(kvp => _prefabHashesToGuidStrings[kvp.Key] = kvp.Value);
}
static void LoadGuidStringsToLocalizedNames()
{
// string resourceName = _localizedLanguages.ContainsKey(_language) ? _localizedLanguages[_language] : "Eclipse.Localization.English.json";
string resourceName = "Eclipse.Localization.English.json";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(resourceName);
using StreamReader localizationReader = new(stream);
string jsonContent = localizationReader.ReadToEnd();
var localizationFile = JsonSerializer.Deserialize<LocalizationFile>(jsonContent);
localizationFile.Nodes
.ToDictionary(x => x.Guid, x => x.Text)
.ForEach(kvp => _guidStringsToLocalizedNames[kvp.Key] = kvp.Value);
}
public static string GetAssetGuidString(PrefabGUID prefabGUID)
{
if (_prefabHashesToGuidStrings.TryGetValue(prefabGUID.GuidHash, out var guidString))
{
return guidString;
}
return string.Empty;
}
public static string GetGuidString(PrefabGUID prefabGUID)
{
if (_prefabHashesToGuidStrings.TryGetValue(prefabGUID.GuidHash, out string guidString))
{
return guidString;
}
return string.Empty;
}
public static string GetNameFromGuidString(string guidString)
{
if (_guidStringsToLocalizedNames.TryGetValue(guidString, out string localizedName))
{
return localizedName;
}
return string.Empty;
}
/*
static string GetLocalizedWords(string message)
{
StringBuilder result = new();
int lastIndex = 0;
foreach (Match match in Regex.Matches(message).Cast<Match>())
{
result.Append(message, lastIndex, match.Index - lastIndex);
if (match.Groups["open"].Success)
{
// Return tags as is
result.Append(match.Value);
}
else if (match.Groups["word"].Success)
{
// Perform case-insensitive replacement for matched words
string word = match.Value;
string wordLower = word.ToLower();
if (LocalizedWords.TryGetValue(wordLower, out string localizedWord))
{
// Preserve the original case of the word
if (char.IsUpper(word[0]))
{
if (localizedWord.Length > 1)
{
localizedWord = char.ToUpper(localizedWord[0]) + localizedWord[1..];
}
else
{
localizedWord = char.ToUpper(localizedWord[0]).ToString();
}
}
result.Append(localizedWord);
}
else
{
result.Append(word); // Use the original case of the word
}
}
lastIndex = match.Index + match.Length;
}
if (lastIndex < message.Length)
{
result.Append(message, lastIndex, message.Length - lastIndex);
}
string translatedMessage = result.ToString();
return translatedMessage;
}
*/
}