Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<system.web> globalization in .net core

I have been using the following setting the the web.config of my previous application.

<system.web>
  <globalization culture="en-AU" uiCulture="en-AU" />
</system.web>

Now in my new .net core project I don't know how to put this setting in the appsettings.json file.

Thanks for your help, Nikolai

like image 686
Nik Avatar asked Dec 16 '16 02:12

Nik


2 Answers

The localization is configured in the Startup class and can be used throughout the application. The AddLocalization method is used in the ConfigureServices to define the resources and localization. This can then be used in the Configure method. Here, the RequestLocalizationOptions can be defined and is added to the stack using the UseRequestLocalization method.

public void ConfigureServices(IServiceCollection services)
{
            services.AddLocalization(options => options.ResourcesPath = "Resources");

            services.AddMvc()
                .AddViewLocalization()
                .AddDataAnnotationsLocalization();

            services.AddScoped<LanguageActionFilter>();

            services.Configure<RequestLocalizationOptions>(
                options =>
                    {
                        var supportedCultures = new List<CultureInfo>
                        {
                            new CultureInfo("en-US"),
                            new CultureInfo("de-CH"),
                            new CultureInfo("fr-CH"),
                            new CultureInfo("it-CH")
                        };

                        options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
                        options.SupportedCultures = supportedCultures;
                        options.SupportedUICultures = supportedCultures;
                    });
}
like image 184
Rahul Nikate Avatar answered Oct 28 '22 12:10

Rahul Nikate


Adding these two lines to ConfigureServices seems to have the effect you want:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {

        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-AU");
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-AU");

I tried approved answer: first of all it needs a LanguageActionFilter class which is not a standard .net core class and after that it did not work for the simple purpose of using preferred culture instead of system default culture.

like image 21
hamid reza Avatar answered Oct 28 '22 11:10

hamid reza