namespace ChatMvc.Middle
{
    using Microsoft.AspNetCore.Http;
    using System.Globalization;
    using System.Threading.Tasks;

    public class RequestLocalizationMiddleware
    {
        private readonly RequestDelegate _next;

        public RequestLocalizationMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            var cookie = context.Request.Cookies["Language"];
            if (!string.IsNullOrEmpty(cookie))
            {
                var culture = new CultureInfo(cookie);
                CultureInfo.CurrentCulture = culture;
                CultureInfo.CurrentUICulture = culture;
            }

            await _next(context);
        }
    }
}
