[英]<system.web> globalization in .net core
I have been using the following setting the the web.config
of my previous application. 我一直在使用以下设置我以前的应用程序的
web.config
。
<system.web>
<globalization culture="en-AU" uiCulture="en-AU" />
</system.web>
Now in my new .net core project I don't know how to put this setting in the appsettings.json
file. 现在,在新的.net核心项目中,我不知道如何将此设置放入
appsettings.json
文件中。
Thanks for your help, Nikolai 感谢您的帮助,尼古拉
The localization is configured in the Startup class
and can be used throughout the application. 本地化在
Startup class
配置,并且可以在整个应用程序中使用。 The AddLocalization
method is used in the ConfigureServices
to define the resources and localization. 在
ConfigureServices
使用AddLocalization
方法定义资源和本地化。 This can then be used in the Configure method. 然后可以在Configure方法中使用它。 Here, the
RequestLocalizationOptions
can be defined and is added to the stack using the UseRequestLocalization
method. 此处,可以使用
UseRequestLocalization
方法定义RequestLocalizationOptions
并将其添加到堆栈中。
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization();
services.AddScoped<LanguageActionFilter>();
services.Configure<RequestLocalizationOptions>(
options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("de-CH"),
new CultureInfo("fr-CH"),
new CultureInfo("it-CH")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
}
Adding these two lines to ConfigureServices seems to have the effect you want: 将这两行添加到ConfigureServices中似乎具有所需的效果:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-AU");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-AU");
I tried approved answer: first of all it needs a LanguageActionFilter class which is not a standard .net core class and after that it did not work for the simple purpose of using preferred culture instead of system default culture. 我尝试了一个经过认可的答案:首先,它需要一个LanguageActionFilter类,它不是标准的.net核心类,其后,出于使用首选区域性而不是系统默认区域性的简单目的,它不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.