Svish Svish - 10 days ago 5
C# Question

Is there a way of setting culture for a whole application? All current threads and new threads?

Is there a way of setting culture for a whole application? All current threads and new threads?

We have the name of the culture stored in a database, and when our application starts, we do

CultureInfo ci = new CultureInfo(theCultureString);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;


But, of course, this gets "lost" when we want to do something in a new thread. Is there a way of setting that
CurrentCulture
and
CurrentUICulture
for the whole application? So that new threads also gets that culture? Or is it some event fired whenever a new thread is created that I can hook up to?

Answer

In .NET 4.5, you can use the CultureInfo.DefaultThreadCurrentCulture property to change the culture of an AppDomain.

For versions prior to 4.5 you have to use reflection to manipulate the culture of an AppDomain. There is a private static field on CultureInfo (m_userDefaultCulture in .NET 2.0 mscorlib, s_userDefaultCulture in .NET 4.0 mscorlib) that controls what CurrentCulture returns if a thread has not set that property on itself.

This does not change the native thread locale and it is probably not a good idea to ship code that changes the culture this way. It may be useful for testing though.

Comments