using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Globalization;
using Microsoft.AspNetCore.Builder;

namespace ASPNETCore_NETCore.Middleware
{
 public static class RequestCultureMiddlewareExtensions
 {
  public static IApplicationBuilder UseRequestCulture(
      this IApplicationBuilder builder)
  {
   return builder.UseMiddleware<RequestCultureMiddleware>();
  }
 }

 public class RequestCultureMiddleware
 {
  private readonly RequestDelegate _next;

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

  public Task Invoke(HttpContext context)
  {
   var cultureQuery = context.Request.Query["culture"];
   if (!string.IsNullOrWhiteSpace(cultureQuery))
   {
    var culture = new CultureInfo(cultureQuery);
    CultureInfo.CurrentCulture = culture;
    CultureInfo.CurrentUICulture = culture;
   }
   // Call the next delegate/middleware in the pipeline
   return this._next(context);
  }
 }
}
