我希望能够单击链接并立即更改我的应用程序的文化/语言。问题是当我点击链接时,没有任何反应。单击确认更改 cookie 值的链接(我检查过)。我在这里错过了什么?
链接:
<a href="/Language/SetLanguage?culture=en [email protected]">English</a>
<a href="/Language/SetLanguage?culture=ms&[email protected]">Malay</a>
控制器:
public IActionResult SetLanguage(string culture, string returnUrl)
{
// Store the selected culture in a cookie or session
Response.Cookies.Append("SelectedCulture", culture);
Response.Cookies.Append("Language", culture);
// Redirect back to the previous page or a specified return URL
return LocalRedirect(returnUrl);
}
我在program.cs中设置服务
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("ms")
};
options.DefaultRequestCulture = new RequestCulture("en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
builder.Services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
var app = builder.Build();
app.UseRequestLocalization(app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);