forked from CHMP-HLab/HLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalisationService.cs
More file actions
222 lines (179 loc) · 6.33 KB
/
Copy pathLocalisationService.cs
File metadata and controls
222 lines (179 loc) · 6.33 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using HLab.Core.Annotations;
using HLab.Mvvm.Annotations;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using OneOf;
using OneOf.Types;
namespace HLab.Mvvm;
public partial class LocalizationService : ILocalizationService, IService
{
CultureInfo Culture { get; set; } = CultureInfo.CurrentCulture;
public string Localize(string value) => Localize(Culture.IetfLanguageTag.ToLowerInvariant(), value);
public Task<string> LocalizeAsync(string value, CancellationToken token = default)
=> LocalizeAsync(Culture.IetfLanguageTag.ToLowerInvariant(), value, token);
public ILocalizeEntry GetLocalizeEntry(string language, string code)
{
foreach (var service in _services)
{
var value = service.GetLocalizeEntry(language, code);
if(value != null ) return value;
}
return null;
}
public async Task<ILocalizeEntry> GetLocalizeEntryAsync(string language, string code, CancellationToken token)
{
foreach (var service in _services)
{
if (token.IsCancellationRequested) return default;
var value = await service.GetLocalizeEntryAsync(language, code).ConfigureAwait(false);
if(value != null ) return value;
}
return null;
}
public IEnumerable<ILocalizeEntry> GetLocalizeEntries(string code)
{
foreach (var service in _services)
{
foreach(var value in service.GetLocalizeEntries(code))
yield return value;
}
}
public async IAsyncEnumerable<ILocalizeEntry> GetLocalizeEntriesAsync(string code, CancellationToken token = default)
{
foreach (var service in _services)
{
await foreach(var value in service.GetLocalizeEntriesAsync(code,token).ConfigureAwait(false))
yield return value;
}
}
/// <summary>
///
/// </summary>
/// <param name="tag">language tag : en,fr,.. </param>
/// <param name="code">"^en=yes|fr=oui"</param>
/// <param name="quality"></param>
/// <returns></returns>
static (string,bool) SelfLocalized(string tag, string code)
{
var quality = false;
if (!code.StartsWith("^")) return (code,quality);
var codes = code.Split('|');
code = codes[0][1..];
var result = code;
foreach (var c in codes.Skip(1))
{
var pos = c.IndexOf('=',StringComparison.InvariantCulture);
if (pos < 0)
{
result = c;
quality = false;
break;
}
if (!c.StartsWith(tag, StringComparison.InvariantCulture)) continue;
result = c[(pos + 1)..];
quality = true;
break;
}
return (result,quality);
}
void Register(string tag, string code, string result, bool quality)
{
foreach (var service in _services)
{
service.Register(tag, code, result, quality);
}
}
public string LocalizeItem(string tag, string code)
{
var (result,quality) = SelfLocalized(tag, code);
foreach (var service in _services)
{
var value = service.Localize(tag, code);
if (value != null) return value;
}
Register(tag, code, result, quality);
return result;
}
public async Task<string> LocalizeItemAsync(string tag, string code, CancellationToken token)
{
foreach (var service in _services)
{
var value = await service.LocalizeAsync(tag, code, token).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(value)) return value;
}
var (result,quality) = SelfLocalized(tag, code);
Register(tag, code, result, quality);
return result;
}
readonly List<ILocalizationProvider> _services = new List<ILocalizationProvider>();
public void Set(CultureInfo info)
{
Culture = info;
}
public void Register(ILocalizationProvider service)
{
_services.Add(service);
}
OneOf<string,NotFound> SelfLocalize(string text, string replaceValue, string tag, string value)
{
var parts = value.Split("=");
if (parts.Length > 1)
{
var p = parts[0].ToLower().Replace("us","en");
if (p == tag || p == tag.Substring(0, 2))
{
return text.Replace(replaceValue, parts[1]);
}
return text.Replace(replaceValue, "");
}
return default(NotFound);
}
public string Localize(string tag, string text)
{
if (string.IsNullOrWhiteSpace(text)) return "";
tag = tag.ToLower();
var matches = InsideBrackets().Matches(text);
foreach (Match match in matches)
{
var value = match.Groups[1].Value;
var bracketValue = match.Value;
text = SelfLocalize(text, bracketValue, tag, value).Match(
t => t,
_ => text.Replace(bracketValue,LocalizeItem(tag, value))
);
}
return text;
}
public async Task<string> LocalizeAsync(string tag, string text, CancellationToken token=default)
{
if (string.IsNullOrWhiteSpace(text)) return "";
tag = tag.ToLower();
var matches = InsideBrackets().Matches(text);
foreach (Match match in matches)
{
var value = match.Groups[1].Value;
var bracketValue = match.Value;
text = await SelfLocalize(text, bracketValue, tag, value).Match<Task<string>>(
Task.FromResult,
async _ => text = text.Replace(bracketValue,await LocalizeItemAsync(tag, value,token))
);
}
return text;
}
public static string KeepLanguage(string text, string lang)
{
return Regex.Replace(text, @"\{" + lang + @"=([\s|!-\|~-■]*)}", "$1");
}
public static string CleanUpLanguage(string text)
{
return Regex.Replace(text,@"\{..=[\s|!-\|~-■]*}", "");
}
public ServiceState ServiceState { get; internal set; } = ServiceState.Available;
[GeneratedRegex("\\{([^}]*)}")]
private static partial Regex InsideBrackets();
}