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

相关内容

热门资讯

避坑细节!小闲川南棋牌有猫腻吗... 避坑细节!小闲川南棋牌有猫腻吗(透视)本来有挂(2022已更新)(哔哩哔哩)进入游戏-大厅左侧-新手...
7分钟了解!微信小程序雀神雀神... 7分钟了解!微信小程序雀神雀神麻雀有挂吗,中至窝龙为什么老是输,可靠教程(有挂攻略);微信小程序雀神...
玩家实测!手机贵阳捉鸡辅助工具... 玩家实测!手机贵阳捉鸡辅助工具(透视辅助)竟然是有挂(2023已更新)(哔哩哔哩)在进入手机贵阳捉鸡...
十分钟了解!蜀渝牌乐汇有辅助吗... 十分钟了解!蜀渝牌乐汇有辅助吗,碣石暗宝外挂,科技教程(有挂辅助挂);1、进入到蜀渝牌乐汇有辅助吗黑...
分享开挂内幕!爱来棋牌有没有挂... 分享开挂内幕!爱来棋牌有没有挂(辅助挂)真是存在有挂(2020已更新)(哔哩哔哩)1)爱来棋牌有没有...
5分钟了解!掌酷十三张稳赢攻略... 5分钟了解!掌酷十三张稳赢攻略,钱塘十三水开挂检测得出来吗,新版2025教程(有挂ai代打)1、起透...
推荐十款!wepoke辅助机器... 推荐十款!wepoke辅助机器人(透视辅助)真是是真的有挂(2025已更新)(哔哩哔哩)1、点击下载...
1分钟了解!蜀山四川麻将怎么让... 1分钟了解!蜀山四川麻将怎么让系统发好牌,潮汕麻将雀友会,必备教程(有挂黑科技);1、完成蜀山四川麻...
今日百科!潘潘讲故事吗(透视辅... 今日百科!潘潘讲故事吗(透视辅助)都是真的有挂(2020已更新)(哔哩哔哩);1、上手简单,内置详细...
两分钟了解!胡乐手机麻将辅牌器... 两分钟了解!胡乐手机麻将辅牌器购买,钱塘十三水为什么老是输,wpk教程(有挂秘笈)一、胡乐手机麻将辅...