Skip to content

Commit

Permalink
feat(src/plugincore.aspnetcore): languageMiddleware: 当前 Language
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Dec 28, 2023
1 parent b50fa81 commit b0d79e7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public static IApplicationBuilder UsePluginCore(this IApplicationBuilder app)
// 一定在 PluginCore 添加的中间件中 第一个
app.UseMiddleware<PluginHttpStartFilterMiddleware>();

app.UseLanguageMiddleware();

app.UsePluginCoreAdminUI();

// 由于没办法在运行时, 动态 UseStaticFiles(), 因此不再为每一个插件都 UseStaticFiles(),
Expand Down
46 changes: 46 additions & 0 deletions src/PluginCore.AspNetCore/Middlewares/LanguageMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===================================================
// License: GNU LGPLv3
// Contributors: yiyungent@gmail.com
// Project: https://yiyungent.github.io/PluginCore
// GitHub: https://github.com/yiyungent/PluginCore
//===================================================



using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using PluginCore.IPlugins;

namespace PluginCore.AspNetCore.Middlewares;

public class LanguageMiddleware
{
private readonly RequestDelegate _next;

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

public async Task Invoke(HttpContext httpContext)
{
// 从 Cookie 中获取语言标识
string language = httpContext.Request.Cookies[Constants.AspNetCoreLanguageCookieName];

// 存储当前语言到 HttpContext.Items
httpContext.Items[Constants.AspNetCoreLanguageKey] = language;

// 调用下一个中间件
await _next(httpContext);
}
}

public static class LanguageMiddlewareExtensions
{
public static IApplicationBuilder UseLanguageMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<LanguageMiddleware>();
}
}

0 comments on commit b0d79e7

Please sign in to comment.