简体   繁体   English

如何将 blazor 服务器端应用程序的“应用程序范围”文化设置为 UK

[英]how do I set the "app wide" culture of a blazor serverside app to UK

Ive been trying (without success) to change the default culture in my Blazor serverside app to UK format.我一直在尝试(没有成功)将我的 Blazor 服务器端应用程序中的默认文化更改为英国格式。 Ive created an extension method on IApplicationBuilder but it doesnt work, dates are always in US format, heres the extension method我在 IApplicationBuilder 上创建了一个扩展方法,但它不起作用,日期总是美国格式,继承人的扩展方法

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

And this is how ive implemented it in the configure method of Startup这就是我在 Startup 的配置方法中实现它的方式

app.UseBrowserLocalisation();

Can anyone see where Im going wrong here?谁能看到我在这里出错了?

**Update I managed to get it working eventually, I added this code to the Configure method in startup **更新我最终设法让它工作,我将此代码添加到启动时的配置方法中

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

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

Then in “ConfigureServices” method of startup.cs add this code:然后在 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;
        });

    }

Configure service for default localization in ConfigureServices then add a middleware in the Configure method of startup.cs like this:在 ConfigureServices 中为默认本地化配置服务,然后在 startup.cs 的 Configure 方法中添加一个中间件,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未在应用中设置文化 - Culture Not Being Set In App 将配置选项卡和固定选项卡(网站)文化设置为与团队应用程序包中的团队相同(C# 和 Blazor) - Set Config Tab and Pinned Tabs(website) culture same as Team in a Teams App Package (C# and Blazor) 如何在 blazor 服务器端应用程序中扩展身份验证过程? - How do I extend the authentication process in a blazor server side app? 如何强制 Blazor 应用程序接受 POST 请求? - How do I force a Blazor app to accept POST requests? 如何通过 IdentityServer4 将 OpenIdConnect 添加到 ASP.NET Core ServerSide Blazor Web 应用程序? - How to add OpenIdConnect via IdentityServer4 to ASP.NET Core ServerSide Blazor web app? 部署 blazor ServerSide 应用程序时服务器上缺少内容 - missing content on server when deployed blazor ServerSide app 如何将 Blazor WebAssembly 应用程序插入任意 HTML 页面或 ASP.NET MVC 5 应用程序? - How do I Insert a Blazor WebAssembly App into an arbitrary HTML Page or ASP.NET MVC 5 app? 如何在Windows应用商店应用中检测设备区域设置? - How can I detect the device culture settings in a Windows Store App? 设置默认区域性MVC 5 Web应用 - Set default culture mvc 5 web app 如何将站点范围的无缓存标头添加到 MVC 3 应用程序 - How do I add site-wide no-cache headers to an MVC 3 app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM