This repository was archived by the owner on Sep 22, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathLocalizationService.cs
More file actions
71 lines (61 loc) · 2.86 KB
/
Copy pathLocalizationService.cs
File metadata and controls
71 lines (61 loc) · 2.86 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
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using LINQtoCSV;
using Timberborn.Common;
using Timberborn.Localization;
using UnityEngine;
namespace MoreLanguages
{
public static class LocalizationService
{
private static readonly string _langDirectory = "lang";
private static readonly string _namePoolFile = "_names";
private static readonly string _langExtension = ".txt";
public static string LangPath => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, _langDirectory);
public static bool TryGetFilePath(string localizationKey, out string localizationFilePath)
{
localizationFilePath = Path.Combine(LangPath, localizationKey + _langExtension);
return File.Exists(localizationFilePath);
}
public static Dictionary<string, string> GetLocalization(string localization)
{
Dictionary<string, string> dictionary = new();
if (TryGetFilePath(localization, out string localizationFilePath))
dictionary.AddRange(TryToReadRecords(localization, localizationFilePath).ToDictionary(record => record.Id, record => TextColors.ColorizeText(record.Text)));
if (TryGetFilePath(localization + _namePoolFile, out string localizationNameFilePath))
dictionary.AddRange(TryToReadRecords(localization + _namePoolFile, localizationNameFilePath).ToDictionary(record => record.Id, record => TextColors.ColorizeText(record.Text)));
return dictionary;
}
/// <summary>
/// Original code from timberborn with a file location instead of stream.
/// Timberborn.Localization.LocalizationRepository.TryToReadRecords
/// </summary>
/// <param name="localization"></param>
/// <param name="filePath"></param>
/// <returns></returns>
/// <exception cref="InvalidDataException"></exception>
public static ImmutableArray<LocalizationRecord> TryToReadRecords(
string localization,
string filePath)
{
try
{
return new CsvContext().Read<LocalizationRecord>(filePath).ToImmutableArray();
}
catch (Exception ex)
{
string message = "Unable to parse file for " + localization + ".";
if (ex is AggregatedException aggregatedException)
message = message + " First error: " + aggregatedException.m_InnerExceptionsList[0].Message;
if (localization == LocalizationCodes.Default)
throw new InvalidDataException(message, ex);
Debug.LogError(message);
return ImmutableArray<LocalizationRecord>.Empty;
}
}
}
}