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?

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.

Setting current culture for entire solution

In .NET Framework 4.5, the CultureInfo.DefaultThreadCurrentCulture Property gets or sets the default culture for threads in the current application domain.

In the .NET Framework 4 and previous versions, by default, the culture
of all threads is set to the Windows system culture. For applications
whose current culture differs from the default system culture, this
behavior is often undesirable. In the .NET Framework 4.5, the
DefaultThreadCurrentCulture property enables an application to define
the default culture of all threads in an application domain.

Set default thread culture for all thread?

if you are using .net 4.5 you can use the below property

CultureInfo.DefaultThreadCurrentCulture Property

In the .NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system culture. For applications whose current culture differs from the default system culture, this behavior is often undesirable. In the .NET Framework 4.5, the DefaultThreadCurrentCulture property enables an application to define the default culture of all threads in an application domain.

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.defaultthreadcurrentculture.aspx

how to set default culture info for entire c# application

Not for entire application or particular class.

CurrentUICulture and CurrentCulture are settable per thread as discussed here Is there a way of setting culture for a whole application? All current threads and new threads?. You can't change InvariantCulture at all.

Sample code to change cultures for current thread:

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

For class you can set/restore culture inside critical methods, but it would be significantly safe to use appropriate overrides for most formatting related methods that take culture as one of arguments:

(3.3).ToString(new CultureInfo("fr-FR"))

WPF Change whole application culture at runtime?

For some reason, the code above changing the culture on the threads/default/dispatchers/window xml works, but when ran from a normal event handler or command.

In my situation, it was run in an async command (with the help of ReactiveUI library ReactiveCommand.CreateFromTask()) and doing it in that context would not keep the changes. Not even if I invoked the WPF dispatcher from inside that task. All following event/commands and even bound property getter would get called with the old culture.

So I can't explain the details, but if your culture changes don't apply, check on which context you are doing the operation.

Also, it is useless to loop on every windows. You can just set it on the Application.Current.MainWindow

How can I change the CurrentCulture of the entire process (not just current thread) in .Net?

You'll have to change the operating system locale if you want to do that. For what reason do you want BackgroundWorkers to run in en-US?

You should have your business layer running in an invariant culture, and only have a specific culture for the end user's UI.

If you are using the BackgroundWorker component, and have to do this you could try something like this in the DoWork method:

// In DoWork
System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
try

{
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en-US");
// Proceed with specific code
}

finally
{
System.Threading.Thread.CurrentThread.CurrentUICulture = before;
}

Is it possible to set the CultureInfo for an .NET application or just a thread?

Unfortunately, every new thread starts with system locale information, even if it's started from a thread that's had its locale changed to something else.

This was a huge gotcha I ran into in one of our apps when using a BackgroundWorker to load a file.

The approach I've used successfully is to set the locale on the startup thread, and then use a thread factory to create threads with the "application locale." For BackgroundWorkers you can use either a factory or a derived class, as Thread is sealed while BackgroundWorker is not.

Thread Creation Event for setting CurrentCulture

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 instead of the system culture. These properties will also set the culture for all existing threads that do no explicitly have their culture set.

Why does Parallel.ForEach change the culture of its threads?

It's because you've set culture for the CurrentThread. Parallel.ForEach will create a new Task for every iteration, which they will have the default culture.

In .NET 4.5, you can use the CultureInfo.DefaultThreadCurrentCulture property to change the culture of an AppDomain ( set the culture for all threads ).

(CultureInfo.DefaultThreadCurrentCulture on msdn)



Related Topics



Leave a reply



Submit