【问题标题】:how do I set the "app wide" culture of a blazor serverside app to UK如何将 blazor 服务器端应用程序的“应用程序范围”文化设置为 UK
【发布时间】:2021-05-28 08:24:50
【问题描述】:

我一直在尝试(没有成功)将我的 Blazor 服务器端应用程序中的默认文化更改为英国格式。我在 IApplicationBuilder 上创建了一个扩展方法,但它不起作用,日期总是美国格式,继承人的扩展方法

public static void UseBrowserLocalisation(this IApplicationBuilder app)
    {
        
        var culture = new CultureInfo("en-GB");
        CultureInfo.CurrentCulture = culture;
        CultureInfo.CurrentUICulture = culture;
        
    }

这就是我在 Startup 的 configure 方法中实现它的方式

app.UseBrowserLocalisation();

谁能看出我哪里出错了?

**更新 我最终设法让它工作,我将此代码添加到启动时的 Configure 方法中

var localizeoptions = new RequestLocalizationOptions()
            .SetDefaultCulture("en-GB")
            .AddSupportedCultures("en-US")
            .AddSupportedUICultures("en-US");
        app.UseRequestLocalization(localizeoptions);

【问题讨论】:

  • 您是否尝试过设置 DefaultThreadCurrentCulture 和 DefaultThreadCurrentUICulture?

标签: c# blazor cultureinfo


【解决方案1】:

首先,使用命名空间“using Microsoft.AspNetCore.Builder;”在 startup.cs 文件中。

然后在startup.cs的“ConfigureServices”方法中添加这段代码:

   public void ConfigureServices(IServiceCollection services)
    {
        
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var CultureInfo = "en-GB";
            var supportedCultures = new[] { new CultureInfo(CultureInfo) };

            options.DefaultRequestCulture = new RequestCulture(CultureInfo);

            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
        });

    }

在 ConfigureServices 中为默认本地化配置服务,然后在 startup.cs 的 Configure 方法中添加一个中间件,如下所示:

app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalization

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 2021-10-21
    • 1970-01-01
    • 2013-05-28
    • 2021-05-15
    • 2020-12-18
    • 2019-12-02
    • 2012-04-26
    相关资源
    最近更新 更多