简体   繁体   English

<system.web> .net核心中的全球化

[英]<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.

相关问题 AdWords SDK 在 .net 核心应用程序中查找 System.Web 参考 - AdWords SDK looks for System.Web reference in .net core app .NET 标准类库中的 System.Web - System.Web in .NET Standard Class Library .NET代码System.Web安装源 - .NET Code System.Web install source .NET 标准中的 HttpContextBase 和 System.Web - HttpContextBase and System.Web in .NET Standard .net 核心 2.2 中的命名空间“System.web”中不存在类型或命名空间名称“HttpBrowserCapabilitiesBase” - the type or namespace name 'HttpBrowserCapabilitiesBase' does not exist in the namespace 'System.web' in .net core 2.2 System.Web可以与带有完整框架的ASP.Net Core一起使用 - Can System.Web be used with ASP.Net Core with Full Framework Request.Url.AbsoluteUri在ASP.NET Core 2.0中不起作用(使用System.Web) - Request.Url.AbsoluteUri not working in asp.net core 2.0 (Using System.Web) ASP.NET Core TypeLoadException无法从程序集“ System.Web”加载类型“ System.Web.PreApplicationStartMethodAttribute” - ASP.NET Core TypeLoadException Could not load type 'System.Web.PreApplicationStartMethodAttribute' from assembly 'System.Web' 可以在Kestrel上自托管剃刀Web应用程序的.NET Core应用程序可以在完整的.NET和参考system.web上运行吗? - Can a .NET Core app, self hosting razor web app on Kestrel, run on full .NET and reference system.web? System.Web 不可用 - System.Web not available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM