Creating a theme for installing CurrentCulture

Our application allows the user to change the Culture in which they work, and this culture may differ from the main OS culture. The only way I found for this is to set Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture for each thread.

The only problem is that we need to establish a culture for each thread, and we need to keep this in mind when we create a new thread. If you install the stream in another culture, then create a new stream, it will receive the OS culture, not the stream that created it.

I would like to find a better way to do this. I can only think of two things, but I do not know if they are possible.

  • Configure the culture at the application level so that all threads default to that culture. Is it possible?
  • Is there a thread creation event anywhere that I can subscribe to? That way, I can configure one handler to set the culture when creating threads.

Any ideas or help would be appreciated even if I need PInvoke prior to the Win32 API. Thanks in advance.

EDIT: I found this question that is similar but no answer found.

+4
source share
3 answers

Two new properties were added to the CultureInfo class in .NET 4.5, which solve this problem, DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture .

Now you can set these two properties, and all new threads will be set to the default culture, and not to the system culture. These properties also establish a culture for all existing streams that clearly do not have their own culture.

0
source

There may not be an exact solution, but what about creating a class responsible for creating the stream?

class MyThreadFactory { public static Thread getThread() { Thread a = new Thread(..); a.CurrentCulture = xxxx; return a; } } 

Using:

 Thread newThread = MyThreadFactory.getThread(); 
+1
source
  • You can put all your culture logic and static resource files into the UI / Presentation layer and just worry about the culture of the user interface threads (assuming you are using a desktop application). This will include any translations that are in the resource files, any formatting specific to a particular culture, etc.
  • If the first idea cannot be implemented, you can create a helper class for the thread that sets the culture for you when creating long threads. You also need to establish a culture of all stream flows.

Culture is a concept specific to threads in Windows, and threads in Windows are actually agnostics of a process or application. There is no level of abstraction between the thread and the OS that contains the culture information (what I know), so you need to set it in each thread.

+1
source

All Articles