-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(src/plugincore.aspnetcore): languageMiddleware: 当前 Language
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/PluginCore.AspNetCore/Middlewares/LanguageMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |