ASP.NET5.0-如何启用CookieRequestCultureProvider?
创始人
2024-09-17 03:30:17
0
  1. 首先,确保在项目中引用了Microsoft.AspNetCore.Localization包。
  2. 在程序启动时,添加以下代码:
services.Configure(
    options => {
        var supportedCultures = new List
        {
            new CultureInfo("en-US"),
            new CultureInfo("fr-FR")
            //添加其他语言文化
        };

        options.DefaultRequestCulture = new RequestCulture("en-US");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;

        //添加CookieRequestCultureProvider
        options.RequestCultureProviders.Insert(0, new CookieRequestCultureProvider());
    });

此代码指定了项目支持的文化信息并添加了CookieRequestCultureProvider。可以根据项目需要添加其他Provider。 3. 在需要使用多语言时,在Controller或视图中添加以下代码:

var currentCulture = HttpContext.Features.Get().RequestCulture.UICulture;

此代码将获取当前用户的语言环境信息,可以将其用于后续处理。

附上完整示例代码: Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Localization;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.AspNetCore.Localization.Routing;

namespace MvcLocalizationDemo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.Configure(
                options => {
                    var supportedCultures = new List
                    {
                        new CultureInfo("en-US"),
                        new CultureInfo("fr-FR")
                    };

                    options.DefaultRequestCulture = new RequestCulture("en-US");
                    options.SupportedCultures = supportedCultures;
                    options.SupportedUICultures = supportedCultures;

                    options.RequestCultureProviders.Insert(0, new RouteData

相关内容

热门资讯

据文件显示!wepoker高级... 据文件显示!wepoker高级辅助,哈糖大菠萝助手(透视)解密教程(有挂分享)-哔哩哔哩据文件显示!...
透视软件!wpk控制牌是真的吗... 透视软件!wpk控制牌是真的吗,wepoker怎么挂底牌(透视)透视教程(有挂方针)-哔哩哔哩1、w...
透视科技!wepoker免费钻... 透视科技!wepoker免费钻石,wepoker有透视底牌吗(透视)开挂教程(确实有挂)-哔哩哔哩1...
透视私人局!fishpoker... 透视私人局!fishpoker透视,聚星ai辅助工具下载(透视)解谜教程(有挂方式)-哔哩哔哩1、下...
透视软件!aapoker脚本,... 透视软件!aapoker脚本,wepoker如何设置透视(透视)总结教程(有挂详情)-哔哩哔哩1、下...
据了解!wpk辅助是真的吗,拱... 据了解!wpk辅助是真的吗,拱趴大菠萝辅助神器(透视)曝光教程(详细教程)-哔哩哔哩1、首先打开拱趴...
透视肯定!wepoker辅助器... 透视肯定!wepoker辅助器,aapoker怎么选牌(透视)解迷教程(真的有挂)-哔哩哔哩1、aa...
透视教程!hhpoker有透视... 透视教程!hhpoker有透视功能吗,德普之星透视辅助插件(透视)了解教程(有挂头条)-哔哩哔哩1、...
透视科技!wepokerplu... 透视科技!wepokerplus脚本,hhpoker有没有辅助(透视)开挂教程(讲解有挂)-哔哩哔哩...
记者获悉!约局吧能不能开挂,a... 您好,约局吧能不能开挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...