-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocalizerMiddleware.cs
68 lines (57 loc) · 2.15 KB
/
LocalizerMiddleware.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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace MtdKey.OrderMaker
{
public class LocalizerMiddleware
{
private readonly RequestDelegate _next;
private readonly IServiceScopeFactory serviceScopeFactory;
public LocalizerMiddleware(RequestDelegate next,IServiceScopeFactory serviceScopeFactory)
{
_next = next;
this.serviceScopeFactory = serviceScopeFactory;
}
public async Task InvokeAsync(HttpContext context)
{
var value = Guid.Empty;
using var scope = serviceScopeFactory.CreateScope();
var language = context.GetBrouserLanguage();
CultureInfo.CurrentCulture = new CultureInfo(language, false);
CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture;
await _next(context);
}
}
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseLocalizerMiddleware(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<LocalizerMiddleware>();
}
public static string GetBrouserLanguage(this HttpContext context)
{
var language = "en-US";
var languages = context.Request.GetTypedHeaders()
.AcceptLanguage
?.OrderByDescending(x => x.Quality ?? 1)
.Select(x => x.Value.ToString())
.ToArray() ?? Array.Empty<string>();
if (languages.Length > 0)
{
language = languages[0];
if (language.ToLower() == "ru" || language.ToLower()[..2] == "ru")
language = "ru-RU";
if (language.ToLower() == "en" || language.ToLower()[..2] == "en")
language = "en-US";
if (language != "ru-RU" && language != "en-US")
language = "en-US";
}
return language;
}
}
}