Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to set CurrentCulture for the entire application?

In a .net 2 winforms application, what's a good way to set the culture for the entire application?
Setting CurrentThread.CurrentCulture for every new thread is repetitive and error-prone.
Ideally I'd like to set it when the app starts and forget about it.

like image 210
Douglas Tosi Avatar asked Sep 18 '08 14:09

Douglas Tosi


People also ask

How do you change the current UI culture?

To change the current UI culture, you assign the CultureInfo object that represents the new UI culture to the Thread. CurrentThread. CurrentUICulture property.

How do you set CultureInfo InvariantCulture?

You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo. InvariantCulture also retrieves an instance of the invariant culture. It can be used in almost any method in the System.

What is the use of CurrentCulture property?

CurrentCulture property is a per-thread setting; that is, each thread can have its own culture. You get the culture of the current thread by retrieving the value of the CultureInfo.


2 Answers

The culture for a thread in .NET is the culture for the system (as viewed by a single application/process). There is no way to override that in .NET, you'll have to continue setting the CurrentCulture for each new thread.

like image 180
Peter Ritchie Avatar answered Sep 28 '22 19:09

Peter Ritchie


You can set application current culture this way:

static void Main()
{
    System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("fi-FI");
    Application.CurrentCulture = cultureInfo;
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

I'm not sure if it helps, because I have never tested it with threads.

edit: it doesn't work. I think you have to set current culture in every thread.

like image 39
Vertigo Avatar answered Sep 28 '22 19:09

Vertigo