-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonStringLocalizer.cs
99 lines (88 loc) · 3.54 KB
/
JsonStringLocalizer.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
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace WebApplication.JSON_Localization
{
public class JsonStringLocalizer : IStringLocalizer
{
private readonly IDistributedCache _cache;
private readonly JsonSerializer _serializer = new JsonSerializer();
public JsonStringLocalizer(IDistributedCache cache)
{
_cache = cache;
}
public LocalizedString this[string name]
{
get
{
string value = GetString(name);
return new LocalizedString(name, value ?? name, value == null);
}
}
public LocalizedString this[string name, params object[] arguments]
{
get
{
var actualValue = this[name];
return !actualValue.ResourceNotFound
? new LocalizedString(name, string.Format(actualValue.Value, arguments), false)
: actualValue;
}
}
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{
string filePath = $"Resources/{Thread.CurrentThread.CurrentCulture.Name}.json";
using (var str = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var sReader = new StreamReader(str))
using (var reader = new JsonTextReader(sReader))
{
while (reader.Read())
{
if (reader.TokenType != JsonToken.PropertyName)
continue;
string key = (string)reader.Value;
reader.Read();
string value = _serializer.Deserialize<string>(reader);
yield return new LocalizedString(key, value, false);
}
}
}
private string GetString(string key)
{
string relativeFilePath = $"Resources/{Thread.CurrentThread.CurrentCulture.Name}.json";
string fullFilePath = Path.GetFullPath(relativeFilePath);
if (File.Exists(fullFilePath))
{
string cacheKey = $"locale_{Thread.CurrentThread.CurrentCulture.Name}_{key}";
string cacheValue = _cache.GetString(cacheKey);
if (!string.IsNullOrEmpty(cacheValue)) return cacheValue;
string result = GetValueFromJSON(key, Path.GetFullPath(relativeFilePath));
if (!string.IsNullOrEmpty(result)) _cache.SetString(cacheKey, result);
return result;
}
return default(string);
}
private string GetValueFromJSON(string propertyName, string filePath)
{
if (propertyName == null) return default;
if (filePath == null) return default;
using (var str = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var sReader = new StreamReader(str))
using (var reader = new JsonTextReader(sReader))
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == propertyName)
{
reader.Read();
return _serializer.Deserialize<string>(reader);
}
}
return default;
}
}
}
}