-
Notifications
You must be signed in to change notification settings - Fork 0
/
DashboardWasmLocalizationProvider.cs
50 lines (44 loc) · 1.97 KB
/
DashboardWasmLocalizationProvider.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
using DevExpress.DashboardWeb;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace BlazorDashboardApp.Client {
public class DashboardWasmLocalizationProvider : IDashboardLocalizationProvider {
static string[] GetJsonList() {
switch (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName) {
case "de":
return new string[] {
"./localization/de/dx-dashboard.de.json",
"./localization/de/dx-analytics-core.de.json"
};
default:
return new string[] { };
}
}
IServiceProvider serviceProvider;
public DashboardWasmLocalizationProvider(IServiceProvider serviceProvider) {
this.serviceProvider = serviceProvider;
}
async Task<Dictionary<string, string>> ReadFileAsync(HttpClient httpClient, string fileName) {
var httpRes = await httpClient.GetAsync(fileName);
if (httpRes.StatusCode == HttpStatusCode.NotFound) {
return new Dictionary<string, string>();
}
var response = await httpRes.Content.ReadAsByteArrayAsync();
return JsonSerializer.Deserialize<Dictionary<string, string>>(response);
}
public async Task<Dictionary<string, string>> GetLocalizationMessagesAsync() {
using (IServiceScope scope = serviceProvider.CreateScope()) {
HttpClient? httpClient = scope.ServiceProvider.GetService<HttpClient>();
var jsons = await Task.WhenAll(GetJsonList().Select(fileName => ReadFileAsync(httpClient, fileName)));
return jsons.SelectMany(dict => dict).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
}
}
}