-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
JsonResourceManager.cs
194 lines (158 loc) · 5.73 KB
/
JsonResourceManager.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
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace My.Extensions.Localization.Json.Internal;
public class JsonResourceManager
{
private readonly JsonFileWatcher _jsonFileWatcher;
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, string>> _resourcesCache = new();
public JsonResourceManager(string resourcesPath, string resourceName = null)
{
ResourcesPath = resourcesPath;
ResourceName = resourceName;
_jsonFileWatcher = new(resourcesPath);
_jsonFileWatcher.Changed += RefreshResourcesCache;
}
public string ResourceName { get; }
public string ResourcesPath { get; }
public string ResourcesFilePath { get; private set; }
public virtual ConcurrentDictionary<string, string> GetResourceSet(CultureInfo culture, bool tryParents)
{
TryLoadResourceSet(culture);
var key = $"{ResourceName}.{culture.Name}";
if (!_resourcesCache.ContainsKey(key))
{
return null;
}
if (tryParents)
{
var allResources = new ConcurrentDictionary<string, string>();
do
{
if (_resourcesCache.TryGetValue(key, out var resources))
{
foreach (var entry in resources)
{
allResources.TryAdd(entry.Key, entry.Value);
}
}
culture = culture.Parent;
} while (culture != CultureInfo.InvariantCulture);
return allResources;
}
else
{
_resourcesCache.TryGetValue(key, out var resources);
return resources;
}
}
public virtual string GetString(string name)
{
var culture = CultureInfo.CurrentUICulture;
GetResourceSet(culture, tryParents: true);
if (_resourcesCache.IsEmpty)
{
return null;
}
do
{
var key = $"{ResourceName}.{culture.Name}";
if (_resourcesCache.TryGetValue(key, out var resources))
{
if (resources.TryGetValue(name, out var value))
{
return value.ToString();
}
}
culture = culture.Parent;
} while (culture != culture.Parent);
return null;
}
public virtual string GetString(string name, CultureInfo culture)
{
GetResourceSet(culture, tryParents: true);
if (_resourcesCache.IsEmpty)
{
return null;
}
var key = $"{ResourceName}.{culture.Name}";
if (!_resourcesCache.TryGetValue(key, out var resources))
{
return null;
}
return resources.TryGetValue(name, out var value)
? value.ToString()
: null;
}
private void TryLoadResourceSet(CultureInfo culture)
{
if (string.IsNullOrEmpty(ResourceName))
{
var file = Path.Combine(ResourcesPath, $"{culture.Name}.json");
TryAddResources(file);
}
else
{
var resourceFiles = Enumerable.Empty<string>();
var rootCulture = culture.Name[..2];
if (ResourceName.Contains('.'))
{
resourceFiles = Directory.EnumerateFiles(ResourcesPath, $"{ResourceName}.{rootCulture}*.json");
if (!resourceFiles.Any())
{
resourceFiles = GetResourceFiles(rootCulture);
}
}
else
{
resourceFiles = GetResourceFiles(rootCulture);
}
foreach (var file in resourceFiles)
{
var fileName = Path.GetFileNameWithoutExtension(file);
var cultureName = fileName[(fileName.LastIndexOf('.') + 1)..];
culture = CultureInfo.GetCultureInfo(cultureName);
TryAddResources(file);
}
}
IEnumerable<string> GetResourceFiles(string culture)
{
var resourcePath = ResourceName.Replace('.', Path.AltDirectorySeparatorChar);
var resourcePathLastDirectorySeparatorIndex = resourcePath.LastIndexOf(Path.AltDirectorySeparatorChar);
var resourceName = resourcePath[(resourcePathLastDirectorySeparatorIndex + 1)..];
var resourcesPath = resourcePathLastDirectorySeparatorIndex == -1
? ResourcesPath
: Path.Combine(ResourcesPath, resourcePath[..resourcePathLastDirectorySeparatorIndex]);
return Directory.Exists(resourcesPath)
? Directory.EnumerateFiles(resourcesPath, $"{resourceName}.{culture}*.json")
: [];
}
void TryAddResources(string resourceFile)
{
var key = $"{ResourceName}.{culture.Name}";
if (!_resourcesCache.ContainsKey(key))
{
var resources = JsonResourceLoader.Load(resourceFile);
_resourcesCache.TryAdd(key, new ConcurrentDictionary<string, string>(resources));
}
}
}
private void RefreshResourcesCache(object sender, FileSystemEventArgs e)
{
var key = Path.GetFileNameWithoutExtension(e.FullPath);
if (_resourcesCache.TryGetValue(key, out var resources))
{
if (!resources.IsEmpty)
{
resources.Clear();
var freshResources = JsonResourceLoader.Load(e.FullPath);
foreach (var item in freshResources)
{
_resourcesCache[key].TryAdd(item.Key, item.Value);
}
}
}
}
}