-
Notifications
You must be signed in to change notification settings - Fork 808
Expand file tree
/
Copy pathLanguageData.cs
More file actions
168 lines (147 loc) · 5.79 KB
/
LanguageData.cs
File metadata and controls
168 lines (147 loc) · 5.79 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
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
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Text.Json.Nodes;
using UniGetUI.Core.Classes;
using UniGetUI.Core.Data;
using UniGetUI.Core.Logging;
using UniGetUI.PackageEngine.Enums;
using Architecture = UniGetUI.PackageEngine.Enums.Architecture;
namespace UniGetUI.Core.Language
{
public static class LanguageData
{
private static Person[]? __translators_list;
public static Person[] TranslatorsList
{
get
{
if (__translators_list is null)
{
__translators_list = LoadLanguageTranslatorList();
}
return __translators_list;
}
}
private static ReadOnlyDictionary<string, string>? __language_reference;
public static ReadOnlyDictionary<string, string> LanguageReference
{
get
{
if (__language_reference is null)
{
__language_reference = LoadLanguageReference();
}
return __language_reference;
}
}
private static ReadOnlyDictionary<string, string>? __translation_percentages;
public static ReadOnlyDictionary<string, string> TranslationPercentages
{
get
{
if (__translation_percentages is null)
{
__translation_percentages = LoadTranslationPercentages();
}
return __translation_percentages;
}
}
private static ReadOnlyDictionary<string, string> LoadTranslationPercentages()
{
try {
if (JsonNode.Parse(File.ReadAllText(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Data", "TranslatedPercentages.json"))) is JsonObject val)
{
return new(val.ToDictionary(x => x.Key, x => (x.Value ?? ("404%" + x.Key)).ToString()));
}
return new(new Dictionary<string, string>());
}
catch (Exception ex)
{
Logger.Error("Could not load TranslatedPercentages.json from disk");
Logger.Error(ex);
return new(new Dictionary<string, string>());
}
}
private static ReadOnlyDictionary<string, string> LoadLanguageReference()
{
try
{
if (JsonNode.Parse(File.ReadAllText(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Data",
"LanguagesReference.json"))) is JsonObject val)
{
return new(val.ToDictionary(x => x.Key, x => (x.Value ?? ("NoNameLang_" + x.Key)).ToString()));
}
return new(new Dictionary<string, string>());
}
catch (Exception ex)
{
Logger.Error("Could not load LanguagesReference.json from disk");
Logger.Error(ex);
return new(new Dictionary<string, string>());
}
}
private static Person[] LoadLanguageTranslatorList()
{
try
{
string JsonContents = File.ReadAllText(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Data",
"Translators.json"));
if (JsonNode.Parse(JsonContents) is not JsonObject TranslatorsInfo)
{
return [];
}
List<Person> result = [];
foreach (KeyValuePair<string, JsonNode?> langKey in TranslatorsInfo)
{
if (!LanguageReference.ContainsKey(langKey.Key))
{
Logger.Warn($"Language {langKey.Key} not in list, maybe has not been added yet?");
continue;
}
JsonArray TranslatorsForLang = (langKey.Value ?? new JsonArray()).AsArray();
bool LangShown = false;
foreach (JsonNode? translator in TranslatorsForLang)
{
if (translator is null)
{
continue;
}
Uri? url = null;
if (translator["link"] is not null && translator["link"]?.ToString() != "")
{
url = new Uri((translator["link"] ?? "").ToString());
}
Person person = new(
Name: (url is not null ? "@" : "") + (translator["name"] ?? "").ToString(),
ProfilePicture: url is not null ? new Uri(url.ToString() + ".png") : null,
GitHubUrl: url,
Language: !LangShown ? LanguageData.LanguageReference[langKey.Key] : ""
);
LangShown = true;
result.Add(person);
}
}
return result.ToArray();
}
catch (Exception ex)
{
Logger.Error("Could not load Translators.json from disk");
Logger.Error(ex);
return [];
}
}
}
public static class CommonTranslations
{
public static readonly Dictionary<string, string> ScopeNames = new()
{
{ PackageScope.Global, "Machine | Global" },
{ PackageScope.Local, "User | Local" },
};
public static readonly Dictionary<string, string> InvertedScopeNames = new()
{
{ "Machine | Global", PackageScope.Global },
{ "User | Local", PackageScope.Local },
};
}
}