DocSpace-buildtools/common/ASC.Api.Core/Middleware/CultureMiddleware.cs

44 lines
1.2 KiB
C#

namespace ASC.Api.Core.Middleware;
public class CultureMiddleware
{
private readonly RequestDelegate _next;
public CultureMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context, UserManager userManager, TenantManager tenantManager, AuthContext authContext)
{
CultureInfo culture = null;
if (authContext.IsAuthenticated)
{
var user = userManager.GetUsers(authContext.CurrentAccount.ID);
if (!string.IsNullOrEmpty(user.CultureName))
{
culture = user.GetCulture();
}
}
if (culture == null)
{
culture = tenantManager.GetCurrentTenant().GetCulture();
}
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
await _next.Invoke(context);
}
}
public static class CultureMiddlewareExtensions
{
public static IApplicationBuilder UseCultureMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CultureMiddleware>();
}
}