4

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; 
Avtandil Kavrelishvili
  • 1,651
  • 3
  • 27
  • 37
  • I've no idea about what `.Net Core` is, but the view may have another way of setting culture, e.g. [wpf has](http://stackoverflow.com/q/7454024/1997232), winforms will require [reloading resources](http://stackoverflow.com/a/7558253/1997232), etc. – Sinatr Apr 03 '17 at 11:54

2 Answers2

2

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).

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • Tasks are not necessarily executed in different threads, but they _might_ be, as decided by the `TaskSchedular` as you said – Jim Aho Jan 16 '20 at 13:16
1

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;
Val
  • 65
  • 2
  • 12