TModLoader v1.4.4.9
TModLoader source code documentation
Loading...
Searching...
No Matches

◆ LoadTranslations()

static List<(string key, string value)> Terraria.ModLoader.LocalizationLoader.LoadTranslations ( Mod mod,
GameCulture culture )
inlinestaticprivate

Definition at line 228 of file LocalizationLoader.cs.

229 {
230 if (mod.File == null)
231 {
232 return new List<(string, string)>();
233 }
234 try
235 {
236 List<(string, string)> flattened = new List<(string, string)>();
237 foreach (TmodFile.FileEntry translationFile in mod.File.Where((TmodFile.FileEntry entry) => Path.GetExtension(entry.Name) == ".hjson"))
238 {
240 if (fileCulture != culture)
241 {
242 continue;
243 }
244 using Stream stream = mod.File.GetStream(translationFile);
246 string translationFileContents = streamReader.ReadToEnd();
247 string modpath = Path.Combine(mod.Name, translationFile.Name).Replace('/', '\\');
248 if (changedFiles.Select(((string Mod, string fileName) x) => Path.Join(x.Mod, x.fileName)).Contains(modpath))
249 {
250 string path2 = Path.Combine(ModCompile.ModSourcePath, modpath);
251 if (File.Exists(path2))
252 {
253 try
254 {
255 translationFileContents = File.ReadAllText(path2);
256 }
257 catch (Exception)
258 {
259 }
260 }
261 }
262 string jsonString;
263 try
264 {
265 jsonString = ((object)HjsonValue.Parse(translationFileContents)).ToString();
266 }
267 catch (Exception e)
268 {
269 throw new Exception("The localization file \"" + translationFile.Name + "\" is malformed and failed to load: ", e);
270 }
271 foreach (JToken t in ((JToken)JObject.Parse(jsonString)).SelectTokens("$..*"))
272 {
273 if (t.HasValues)
274 {
275 continue;
276 }
277 JObject obj = (JObject)(object)((t is JObject) ? t : null);
278 if (obj != null && ((JContainer)obj).Count == 0)
279 {
280 continue;
281 }
282 string path = "";
283 JToken current = t;
284 for (JToken parent = (JToken)(object)t.Parent; parent != null; parent = (JToken)(object)parent.Parent)
285 {
286 JProperty property = (JProperty)(object)((parent is JProperty) ? parent : null);
287 string text;
288 if (property == null)
289 {
290 JArray array = (JArray)(object)((parent is JArray) ? parent : null);
291 text = ((array == null) ? path : (array.IndexOf(current) + ((path == string.Empty) ? string.Empty : ("." + path))));
292 }
293 else
294 {
295 text = property.Name + ((path == string.Empty) ? string.Empty : ("." + path));
296 }
297 path = text;
298 current = parent;
299 }
300 path = path.Replace(".$parentVal", "");
301 if (!string.IsNullOrWhiteSpace(prefix))
302 {
303 path = prefix + "." + path;
304 }
305 flattened.Add((path, ((object)t).ToString()));
306 }
307 }
308 return flattened;
309 }
310 catch (Exception ex2)
311 {
312 ex2.Data["mod"] = mod.Name;
313 throw;
314 }
315 }
static readonly string ModSourcePath
Definition ModCompile.cs:61
static GameCulture string prefix GetCultureAndPrefixFromPath(string path)
static readonly HashSet<(string Mod, string fileName)> changedFiles
static GameCulture culture
Derives a culture and shared prefix from a localization file path. Prefix will be found after culture...

References Terraria.ModLoader.LocalizationLoader.changedFiles, Terraria.ModLoader.LocalizationLoader.culture, Terraria.ModLoader.Mod.File, Terraria.ModLoader.LocalizationLoader.GetCultureAndPrefixFromPath(), Terraria.ModLoader.Core.TmodFile.GetStream(), Terraria.ModLoader.Core.ModCompile.ModSourcePath, and Terraria.ModLoader.Mod.Name.

Referenced by Terraria.ModLoader.LocalizationLoader.Autoload(), Terraria.ModLoader.LocalizationLoader.GetLocalizationCounts(), and Terraria.ModLoader.LocalizationLoader.LoadModTranslations().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: