ASP.NET MVC Core如何获取应用程序支持的文化列表 [英] ASP.NET MVC Core how to get application supported culture list

查看:82
本文介绍了ASP.NET MVC Core如何获取应用程序支持的文化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Startup.cs中,我添加了两种文化:

In my Startup.cs I added two cultures:

       var cultureLt = new CultureInfo("LT");
       var cultureEn = new CultureInfo("EN");
       var supportedCultures = new List<CultureInfo> {cultureEn, cultureLt};

       var requestLocalizationOptions = new RequestLocalizationOptions();
       requestLocalizationOptions.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider());
       requestLocalizationOptions.SupportedCultures = supportedCultures;
       requestLocalizationOptions.SupportedUICultures = supportedCultures;

       app.UseRequestLocalization(requestLocalizationOptions);

我需要在构造函数中获取此列表,现在在构造控制器中,我已初始化变量

I need to get this list in constructor and now in consturctor controller I initialized variable

private readonly IOptions<RequestLocalizationOptions> _locOptions;

在行动中,我试图获得如下列表:

and in Action I'm trying to get this list like this:

var cultureItems = _locOptions.Value.SupportedUICultures
            .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
            .ToList();

但是问题是该行仅返回当前在应用程序中设置的区域性...如何同时获取EN和LT文化?

but the problem is that this line only returns the culture that is currently set in application... How to get both EN and LT cultures?

推荐答案

您必须配置RequestLocalizationOptions.

You must configure the RequestLocalizationOptions.

public void ConfigureServices(IServiceCollection services)
{         
     // ... enter code here

     // RequestLocalizationOptions must to be configured 
     var cultureLt = new CultureInfo("LT");
     var cultureEn = new CultureInfo("EN");
     var supportedCultures = new[] { cultureEn, cultureLt };

     services.Configure<RequestLocalizationOptions>(options =>
     {
         options.SupportedCultures = supportedCultures;
         options.SupportedUICultures = supportedCultures;
     });

     // Add them to IServiceCollection
     services.AddLocalization();

     // ... enter code here
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // ... enter code here

    // add RequestLocalizationMiddleware to pipeline
    app.UseRequestLocalization();

    app.UseMvc...
}

这篇关于ASP.NET MVC Core如何获取应用程序支持的文化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆