我跟着關於在Asp.Net核心本地化的微軟教程,我有一個問題,文化不會改變,除非我更改Startup.cs文件中的默認文化。Asp.net核心1.1本地化,不改變文化
// Startup.cs - ConfigureServices Method
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
// Startup.cs - Configure Method
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-GB"),
new CultureInfo("fr-FR"),
...
};
app.UseRequestLocalization(new RequestLocalizationOptions()
{
DefaultRequestCulture = new RequestCulture("en-GB"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
RequestCultureProviders = new List<IRequestCultureProvider>()
});
// HomeController
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
,我想用一個錨鏈接來改變語言,如:
<a asp-area="" asp-controller="Home" asp-action="SetLanguage" asp-route-culture="fr-FR" asp-route-returnUrl="@Context.Request.Path">FR</a>
當我點擊的cookie成功是越來越設置鏈接,頁面重新加載,但本地化的文本仍然是相同。當我在Startup.cs文件中更改DefaultRequestCulture時,文化顯得很好(這意味着我的Resx文件已正確命名等)
那麼,如何才能實現基於點擊鏈接來更改語言?
編輯 我仍然在尋找一個解決方案,如果有人能幫助
EDIT 2 我做了一個新的解決方案,但問題仍然發生在VS 2017年,我上載我的VS項目Github上,如果任何人都可以採取更好看,並解釋我什麼我做錯了https://github.com/adonis07/Localization
編輯3
我能夠通過添加NuGet包中要解決的問題我解決方案Microsoft.AspNetCore.Authentication.Cookies。現在文化運作正常。
是指這樣的:https://stackoverflow.com/a/38499465/5417285 –