How to set the locale for an ASP.NET 5 application?

I am trying to make a host locale override cover for an ASP.NET 5 web application. Most of the solutions relate to the <globalization/>web.config element , but this is specific to IIS and does not seem to fit the new ASP.NET model.

I tried:

app.Use(next => context => {
    Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-AU");
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-AU");
    return next(context);
});

It does, but it doesn’t affect the request (perhaps due to the extensive Tasking in the pipeline?) Is there a better way to accomplish this?

+3
source share
2 answers

The problem is with the asynchronous controller. Instead, you should set the default culture for all threads:

app.Use(next => context => {
    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-AU");
    CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-AU");

    return next(context);
});

You can simply put these lines at the beginning of the Configure method:

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-AU");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-AU");
+7

ASP.NET 5, , Damian Edwards github

, 21 2015 .

beta6. .

27 2015 .

, aspnet/Localization

+5

Source: https://habr.com/ru/post/1666753/


All Articles