Localization 默认情况下,使用ASPNETCore 1.1.1本地化将当前区域性设置为浏览器区域性
我想知道如何使用ASPNETCore 1.1.1将当前区域性默认设置为浏览器区域性 我遵循这个例子 在我的startup.cs中,我有这个Localization 默认情况下,使用ASPNETCore 1.1.1本地化将当前区域性设置为浏览器区域性,localization,core,globalization,culture,Localization,Core,Globalization,Culture,我想知道如何使用ASPNETCore 1.1.1将当前区域性默认设置为浏览器区域性 我遵循这个例子 在我的startup.cs中,我有这个 public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IStringLocalizerFactory, SingleFileResourceManagerStringLocalizerFactory>(); servi
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IStringLocalizerFactory, SingleFileResourceManagerStringLocalizerFactory>();
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
// Add framework services.
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en"),
new CultureInfo("es"),
new CultureInfo("fr"),
};
opts.DefaultRequestCulture = new RequestCulture("fr");
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
});
}
这是资源文件
- Resources.resx Resources.en.resx 参考资料 Resources.fr.resx
Jolynice当用户离开网站时,cookie不会消亡,因此,由于您将cookie的过期时间设置为一年,因此用户选择的最后一种语言将在一年内生效 与Cookie不同,当用户离开网站时,会话就会终止,而具有相同需求的人已经在github上请求了该功能,并为其创建了NuGet包
解决此问题的其他方法是使用更短的过期日期(如果您不需要非常精确),或者在检测到用户创建了新会话时删除cookie。谢谢Vache,我感谢您的支持。
[HttpPost]
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);
}