65

I want to set default culture info for that class or for entire application.

For example in Turkey 3,2 = in english 3.2

so application uses my local but i want it to use as default

System.Globalization.CultureInfo.InvariantCulture

How can i set it to that as default for that specific class or for entire application

svick
  • 236,525
  • 50
  • 385
  • 514
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • 2
    I have to add that the non-invariant versions of .ToString() are much slower, and that "Thread.CurrentThread.CurrentCulture" does not make a difference. – gatopeich Jun 14 '13 at 15:13
  • 1
    Possible duplicate of [Is there a way of setting culture for a whole application? All current threads and new threads?](http://stackoverflow.com/questions/468791/is-there-a-way-of-setting-culture-for-a-whole-application-all-current-threads-a) – William Gross Apr 19 '17 at 12:43

3 Answers3

67

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"))
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    so how do i set it for the form window that i am working on. it is not recognizing the thread – Furkan Gözükara Nov 13 '12 at 01:13
  • 1
    i am not really interested in defining for each transition. isn't that possible to set it for the form thread. you know when you create a new window form project ? – Furkan Gözükara Nov 13 '12 at 01:16
  • 2
    ok i found how to make : System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US", false); System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false); – Furkan Gözükara Nov 13 '12 at 01:19
58

With 4.0, you will need to manage this yourself by setting the culture for each thread as Alexei describes. But with 4.5, you can define a culture for the appdomain and that is the preferred way to handle this. The relevant apis are CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture.

Eric MSFT
  • 3,246
  • 1
  • 18
  • 28
1

If you use a Language Resource file to set the labels in your application you need to set the its value:

CultureInfo customCulture = new CultureInfo("en-US");
Languages.Culture = customCulture;
Renzo Ciot
  • 3,746
  • 2
  • 25
  • 29