-
Notifications
You must be signed in to change notification settings - Fork 8
/
StringUtils.cs
273 lines (237 loc) · 8.36 KB
/
StringUtils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using KMod;
using STRINGS;
namespace RomenH.Common
{
/// <summary>
/// A class that provides static utility methods for strings.
/// </summary>
public static class StringUtils
{
private static Dictionary<string, LocString> registeredStrings = new Dictionary<string, LocString>();
public static LocString BuildingName(string ID, string value)
{
return new LocString(UI.FormatAsLink(value, ID.ToUpperInvariant()), "STRINGS.BUILDINGS.PREFABS." + ID.ToUpperInvariant() + ".NAME");
}
public static LocString BuildingDesc(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID.ToUpperInvariant() + ".DESC");
}
public static LocString BuildingEffect(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID.ToUpperInvariant() + ".EFFECT");
}
public static LocString BuildingLogicPortName(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID.ToUpperInvariant() + ".LOGIC_PORT");
}
public static LocString BuildingLogicPortActive(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID.ToUpperInvariant() + ".LOGIC_PORT_ACTIVE");
}
public static LocString BuildingLogicPortInactive(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID.ToUpperInvariant() + ".LOGIC_PORT_INACTIVE");
}
public static LocString CreatureName(string ID, string value)
{
return new LocString(value, "STRINGS.CREATURES.SPECIES." + ID.ToUpperInvariant() + ".NAME");
}
public static LocString CreatureDesc(string ID, string value)
{
return new LocString(value, "STRINGS.CREATURES.SPECIES." + ID.ToUpperInvariant() + ".DESC");
}
public static LocString CreatureVariantName(string ID, string variant, string value)
{
return new LocString(value, "STRINGS.CREATURES.SPECIES." + ID.ToUpperInvariant() + "." + variant.ToUpperInvariant() + ".NAME");
}
public static LocString CreatureVariantDesc(string ID, string variant, string value)
{
return new LocString(value, "STRINGS.CREATURES.SPECIES." + ID.ToUpperInvariant() + "." + variant.ToUpperInvariant() + ".DESC");
}
public static LocString StatusItemName(string ID, string prefix, string value)
{
return new LocString(value, "STRINGS." + prefix.ToUpperInvariant() + ".STATUSITEMS." + ID.ToUpperInvariant() + ".NAME");
}
public static LocString StatusItemTooltip(string ID, string prefix, string value)
{
return new LocString(value, "STRINGS." + prefix.ToUpperInvariant() + ".STATUSITEMS." + ID.ToUpperInvariant() + ".TOOLTIP");
}
public static LocString LogicPortDesc(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID + ".LOGIC_PORT");
}
public static LocString LogicPortInputActive(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID + ".INPUT_PORT_ACTIVE");
}
public static LocString LogicPortOutputActive(string ID, string value)
{
return new LocString(value, "STRINGS.BUILDINGS.PREFABS." + ID + ".INPUT_PORT_INACTIVE");
}
public static LocString TechName(string ID, string value)
{
return new LocString(value, "STRINGS.RESEARCH.TECHS." + ID.ToUpperInvariant() + ".NAME");
}
public static LocString TechDesc(string ID, string value)
{
return new LocString(value, "STRINGS.RESEARCH.TECHS." + ID.ToUpperInvariant() + ".DESC");
}
internal static void RegisterAllLocStrings()
{
try
{
ModCommon.Log.Debug("Searching for translatable strings...");
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
{
if (type?.FullName?.StartsWith("RomenH.") ?? false)
{
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly))
{
if (field.FieldType == typeof(LocString))
{
LocString ls = (LocString)field.GetValue(null);
if (ls.key.IsValid())
{
ModCommon.Log.Debug($"Found string: {ls.key.String}");
Strings.Add(ls.key.String, ls.text);
registeredStrings.Add(ls.key.String, ls);
}
}
}
}
}
}
catch (Exception ex)
{
ModCommon.Log.Error("Failed to register strings.", ex);
}
try
{
ModCommon.Log.Debug("Exporting translation template...");
WritePOTemplate(ModCommon.Folder);
}
catch (Exception ex)
{
ModCommon.Log.Error("Failed to export translation template.", ex);
}
ModCommon.Log.Debug("RegisterAllLocStrings End");
}
public static void WritePOTemplate(string modFolder)
{
try
{
ModCommon.Log.Debug("Writing pot file...");
string translationsFolder = Path.Combine(modFolder, "Translations");
if (!Directory.Exists(translationsFolder))
{
Directory.CreateDirectory(translationsFolder);
}
string file = Path.Combine(translationsFolder, "en.pot");
using (StreamWriter writer = new StreamWriter(file, false, new UTF8Encoding(false)))
{
writer.WriteLine("msgid \"\"");
writer.WriteLine("msgstr \"\"");
writer.WriteLine("\"Application: Oxygen Not Included\"");
writer.WriteLine("\"POT Version: 2.0\"");
writer.WriteLine("");
foreach (var kvp in registeredStrings)
{
string key = kvp.Key;
string value = kvp.Value;
value = value.Replace("\"", "\\\"");
value = value.Replace("\n", "\\n");
writer.WriteLine("#. " + key);
writer.WriteLine("msgctxt \"{0}\"", key);
writer.WriteLine("msgid \"" + value + "\"");
writer.WriteLine("msgstr \"\"");
writer.WriteLine("");
}
}
}
catch (Exception ex)
{
ModCommon.Log.Error("RomenH.CommonLib: Failed to write po translations template.", ex);
}
ModCommon.Log.Debug("WritePOTemplate End");
}
public static void ApplyTranslationsPatch(Harmony harmony)
{
try
{
ModCommon.Log.Debug("Applying translation loader patch...");
var targetMethod = typeof(Localization).GetMethod(nameof(Localization.Initialize));
HarmonyMethod postfixPatch = new HarmonyMethod(typeof(StringUtils), nameof(LoadTranslations));
harmony.Patch(targetMethod, postfix: postfixPatch);
}
catch (Exception ex)
{
ModCommon.Log.Error("Failed to apply translation patch.", ex);
}
ModCommon.Log.Debug("ApplyTranslationsPatch End");
}
public static void LoadTranslations()
{
try
{
ModCommon.Log.Debug("Loading translations...");
string localeCode = Localization.GetLocale()?.Code ?? Localization.DEFAULT_LANGUAGE_CODE;
ModCommon.Log.Debug($"Language code is {localeCode}.");
if (localeCode == Localization.DEFAULT_LANGUAGE_CODE) return;
// Try to find the .po file...
string translationsFolder = Path.Combine(ModCommon.Folder, "Translations");
if (!Directory.Exists(translationsFolder))
{
ModCommon.Log.Warn("Translations folder not found. This mod will not be translated.");
return;
}
else
{
ModCommon.Log.Debug("Translations folder found.");
}
string locPOFile = Path.Combine(translationsFolder, $"{localeCode}.po");
if (File.Exists(locPOFile))
{
ModCommon.Log.Debug($"Translation file is: {locPOFile}");
// Load the .po file and overwrite strings
var strings = Localization.LoadStringsFile(locPOFile, false);
if (strings.Count == 0)
{
ModCommon.Log.Warn("Translation file has no strings. This mod will not be translated.");
return;
}
FieldInfo locStringTextField = typeof(LocString).GetField("_text", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var kvp in strings)
{
if (registeredStrings.TryGetValue(kvp.Key, out LocString locStr))
{
ModCommon.Log.Debug($"Applying translation for : {kvp.Key}");
locStringTextField.SetValue(locStr, kvp.Value);
Strings.Add(kvp.Key, kvp.Value);
}
else
{
ModCommon.Log.Debug($"LocString not found for: {kvp.Key}");
}
}
ModCommon.Log.Info($"Loaded translations file: {localeCode}.po");
}
else
{
ModCommon.Log.Info($"No translations file found for current locale. ({localeCode})");
}
}
catch (Exception ex)
{
ModCommon.Log.Error("Failed to load translations file.", ex);
}
}
}
}