.NET Core中的<system.web>全球化

7

我曾在之前的应用程序中使用以下设置于 web.config 文件中。

<system.web>
  <globalization culture="en-AU" uiCulture="en-AU" />
</system.web>

在我的新的 .net core 项目中,我不知道如何将此设置放入 appsettings.json 文件中。

感谢您的帮助, Nikolai


可能的解决方案:https://dev59.com/KFkS5IYBdhLWcg3wDyn5#72435434 - Hicham O-Sfh
2个回答

7
本地化配置是在 Startup 类中进行配置的,可以在整个应用程序中使用。 在 ConfigureServices 中使用 AddLocalization 方法来定义资源和本地化。 然后可以在 Configure 方法中使用它。 在这里,可以定义 RequestLocalizationOptions 并使用 UseRequestLocalization 方法将其添加到堆栈中。
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;
                    });
}

1
谢谢这个。 :) - Nik

3

在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");

我尝试了一个被认可的答案:首先需要一个LanguageActionFilter类,这不是标准的 .net core 类,然后它并没有实现使用首选语言而不是系统默认语言的简单目的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接