Asp.netcore不会改变CultureInfo
创始人
2024-09-17 19:01:07
0

需要手动更改 CultureInfo。可以在 Startup.cs 文件的 ConfigureServices 方法中添加以下代码:

services.Configure(options =>
{
    var supportedCultures = new List
    {
        new CultureInfo("en-US"), // 美式英语
        new CultureInfo("zh-CN")  // 简体中文
    };
    options.DefaultRequestCulture = new RequestCulture(culture: "zh-CN", uiCulture: "zh-CN");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
    options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
    {
        var userLangs = context.Request.Headers["Accept-Language"].ToString();
        var culture = CultureInfo.DefaultThreadCurrentCulture;
        if (!string.IsNullOrEmpty(userLangs))
        {
            var userLang = userLangs.Split(',').FirstOrDefault();
            if (!string.IsNullOrEmpty(userLang))
            {
                try
                {
                    culture = new CultureInfo(userLang);
                }
                catch (CultureNotFoundException)
                {
                    // ignore invalid culture
                }
            }
        }
        return Task.FromResult(new ProviderCultureResult(culture.Name));
    }));
});

此代码中:

  • DefaultRequestCulture 属性表示默认的请求区域设置,即当请求中没有指定区域设置时,将根据此处设置的值来确定。
  • SupportedCultures 属性表示支持的区域设置列表。
  • SupportedUICultures 属性表示 UI 支持的区域设置列表。
  • RequestCultureProviders 属性表示提供区域设置的提供程序列表。

此处还使用了一个 CustomRequestCultureProvider,在请求时根据 "Accept-Language" 请求头的值来确定区域设置。

更多关于如何支持多国语言的内容,请参考 Microsoft 的官方文档。

相关内容

热门资讯

分享个大家!wepoker数据... 分享个大家!wepoker数据分析工具,pokerrrr2辅助,其实是有挂(真的有挂)1、辅助软件下...
三分钟了解!aapoker怎么... 三分钟了解!aapoker怎么控制牌,哈糖大菠萝开挂,原来有挂(竟然有挂)1、进入到是否有挂之后,能...
专业讨论!德州局hhpoker... 专业讨论!德州局hhpoker,wepoker辅助透视,竟然存在有挂(有挂教程)专业讨论!德州局hh...
科技揭秘!wepoker辅助透... 科技揭秘!wepoker辅助透视软件,菠萝德普辅助器免费版在哪里,本来真的是有挂(有挂教程)科技揭秘...
必备科技!德普之星透视软件免费... 必备科技!德普之星透视软件免费入口官网,wepoker网页版透视方法,其实是真的有挂(有挂方式)1、...
盘点十款!pokemmo辅助器... 盘点十款!pokemmo辅助器,德州透视是真的假的,都是真的有挂(真实有挂)1、起透看视 辅助软件价...
热点讨论!wpk刷入池率脚本,... 热点讨论!wpk刷入池率脚本,约局吧辅助器,一直是有挂(有挂功能)1、超多福利:超高返利,海量正版游...
最新研发!hhpoker有没有... 最新研发!hhpoker有没有作弊辅助,wepoker有没有插件,确实有挂(有挂秘诀)1)有没有挂:...
我来教大家!wejoker私人... 我来教大家!wejoker私人辅助软件,aapoker插件,其实是真的有挂(有挂解密)1、公共底牌简...
玩家亲测!xpoker辅助怎么... 玩家亲测!xpoker辅助怎么用,德扑圈透视,真是是真的有挂(有挂头条)1)有没有挂:进一步探索有辅...