Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set culture and ui-culture in appsettings.json (asp.net core localization)?

I have asp.net core application which I want to localize and translate (v 1.1.0). I want the source code to support both english and norwegian deployments, and I found this could be a startup.cs configuration

RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
{
    SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("nb-NO") },
    SupportedUICultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("nb-NO") },
    DefaultRequestCulture = new RequestCulture("nb-NO")
};
app.UseRequestLocalization(localizationOptions);

However I want to have only one culture and one ui-culture for each deployment, so I hoped to find out a way to set culture and ui-culture in appsettings.json or enviroment variables. Is it possible?

like image 412
Ole Kristian Losvik Avatar asked Dec 26 '16 22:12

Ole Kristian Losvik


People also ask

How will you set the culture and UI culture for ASP.NET web page globalization?

To have ASP.NET set the UI culture and culture to the first language that is specified in the current browser settings, set UICulture and Culture to auto. Alternatively, you can set this value to auto:culture_info_name, where culture_info_name is a culture name. For a list of culture names, see CultureInfo.

What is globalization and localization in C#?

Globalization is the process of designing and developing applications that function for multiple cultures. Localization is the process of customizing your application for a given culture and locale.

What is the use of Appsettings json in .NET Core?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.


1 Answers

Sure.

appsettings.json:

{
    "SiteLocale" : "en-US"
}

Startup.cs:

var locale = Configuration["SiteLocale"];
RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
{
    SupportedCultures = new List<CultureInfo> { new CultureInfo(locale) },
    SupportedUICultures = new List<CultureInfo> { new CultureInfo(locale) },
    DefaultRequestCulture = new RequestCulture(locale)
};
app.UseRequestLocalization(localizationOptions);
like image 90
Dmitry Avatar answered Sep 19 '22 11:09

Dmitry