35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using EnvelopeGenerator.Web.Controllers;
|
|
using EnvelopeGenerator.Web.Models;
|
|
using Microsoft.AspNetCore.Localization;
|
|
using Microsoft.Extensions.Options;
|
|
using System.Globalization;
|
|
|
|
namespace EnvelopeGenerator.Web.Middleware;
|
|
|
|
public class CultureMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
private readonly Cultures _cultures;
|
|
|
|
public CultureMiddleware(RequestDelegate next, IOptions<Cultures> culturesOpt)
|
|
{
|
|
_next = next;
|
|
_cultures = culturesOpt.Value;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext context)
|
|
{
|
|
var cookieName = CookieRequestCultureProvider.DefaultCookieName;
|
|
var cookieValue = context.Request.Cookies[cookieName];
|
|
|
|
if (string.IsNullOrEmpty(cookieValue))
|
|
{
|
|
context.Response.Cookies.SetCulture(_cultures.Default.Language);
|
|
CultureInfo.CurrentCulture = new CultureInfo(_cultures.Default.Language);
|
|
CultureInfo.CurrentUICulture = new CultureInfo(_cultures.Default.Language);
|
|
}
|
|
|
|
await _next(context);
|
|
}
|
|
}
|