-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalizationMiddleware.cs
33 lines (30 loc) · 1.09 KB
/
LocalizationMiddleware.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
using Microsoft.AspNetCore.Http;
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace WebApplication.JSON_Localization
{
public class LocalizationMiddleware : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var cultureKey = context.Request.Headers["Accept-Language"];
if (!string.IsNullOrEmpty(cultureKey))
{
if (DoesCultureExist(cultureKey))
{
var culture = new System.Globalization.CultureInfo(cultureKey);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
await next(context);
}
private static bool DoesCultureExist(string cultureName)
{
return CultureInfo.GetCultures(CultureTypes.AllCultures).Any(culture => string.Equals(culture.Name, cultureName, StringComparison.CurrentCultureIgnoreCase));
}
}
}