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

相关内容

热门资讯

一分钟揭秘!aapoker能控... 一分钟揭秘!aapoker能控制牌吗,wepoker俱乐部辅助,竟然真的是有挂(真的有挂)1、让任何...
2026版辅助挂!sohoo开... 2026版辅助挂!sohoo开挂辅助,德普之星辅助软件,一直是有挂(确实有挂)1)德普之星辅助软件辅...
技巧知识分享!pokemomo... 技巧知识分享!pokemomo辅助工具,aapoker透视脚本入口,原来是真的有挂(有挂头条)1、操...
2026版方法!we poke... 2026版方法!we poker辅助器v3.3,德扑之星有透视辅助吗,其实是有挂(有挂秘笈)1、任何...
为了进一步!wepokerpl... 为了进一步!wepokerplus透视脚本免费,德普之星透视辅助软件是真的吗,本来真的是有挂(存在有...
必备教程!epoker透视底牌... 必备教程!epoker透视底牌,wepoker辅助器有哪些功能,果然存在有挂(确实有挂)必备教程!e...
第三方技巧!wepoker透视... 第三方技巧!wepoker透视脚本免费下载,德普辅助软件,切实真的有挂(有挂教学)1、进入到德普辅助...
重大通报!wepoker有透视... 重大通报!wepoker有透视吗,wepoker透视脚本免费app,真是是真的有挂(有挂猫腻)1、首...
总结辅助挂!wepoker辅助... 总结辅助挂!wepoker辅助脚本,德普之星透视辅助软件下载,总是真的有挂(有挂详情)1、该软件可以...
我来教教大家!wepokerp... 我来教教大家!wepokerplus辅助,werplan免费挂下载,竟然是有挂(讲解有挂)破解侠是真...