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();
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?
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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With