Avtandil Kavrelishvili
Avtandil Kavrelishvili

Reputation: 1757

Why is Culture Info different in controller and view in .NET Core MVC

I think .Net Core's community and documentation regarding Localization is poor. That's why I have some problem about it.

When I change 'Culture info' in Controller (see code below) is working well, but after that when I check 'culture info' in view is different. Please assist me to fix this issue.

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo('en-GB');

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture; 

Upvotes: 4

Views: 5675

Answers (2)

Val
Val

Reputation: 65

You may want to check this properties:

var cultureInfo = new System.Globalization.CultureInfo('en-GB');
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

Upvotes: 1

Joel Harkes
Joel Harkes

Reputation: 11671

Why?

Because dotnet core it is more focused about async coding meaning Tasks are run per part. So controller action is 1 task. creating/executing the view is another task (or multiple tasks).

Tasks are run in different threads (by the TaskSchedular) using a pool (re-using threads when task is finished). And thus there is no guarentee it is run in the same Thread.

How handle cultures?

Take a look at the documentation: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization. They provide perfect insight in how it works. (They tell you to use: CultureInfo.CurrentCulture but please read the documentation).

Upvotes: 2

Related Questions