Domanda

Hellu

I'm trying to translate my winforms application, but it contains numerous uses of threading and background workers.

The fact is that my program doesn't seem to use the right resx file when I call it inside a thread that is not the UI one (which, in my case, means having french text in a log window while the UI is in english).

The question is: Is there a way to apply a culture to every thread called by my application in a single place?

È stato utile?

Soluzione

Set CultureInfo.DefaultThreadCurrentUICulture

Gets or sets the default UI culture for threads in the current application domain.

and

Important If you have not explicitly set the UI culture of any existing threads executing in an application domain, setting the DefaultThreadCurrentUICulture property also changes the culture of these threads

But:

if these threads execute in another application domain, their culture is defined by the DefaultThreadCurrentUICulture property in that application domain or, if no default value is defined, by the default system culture. Because of this, we recommend that you always explicitly set the culture of your main application thread and do not rely on the DefaultThreadCurrentUICulture property to define the culture of the main application thread.

So, at start up, on the main thread, set both the CultureInfo.CurrentUICulture and CultureInfo.DefaultThreadCurrentUICulture.

And similarly for CultureInfo.DefaultThreadCurrentCulture and CultureInfo.CurrentCulture.

None of this will work for operations in threads where the thread either sets its current (UI) culture or explicitly passes a IFormatProvider to formatting methods or a CultureInfo to resource getting methods.

Also, both DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture were added with .NET 4.5, if you need to target an older version then you are stuck with either setting CurrentCulture and CurrentUICulture at every thread start (or pool entry point) or passing an explicit culture for every method that uses locale information (FXCop will help here: can warn for all method calls where this is not done).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top