Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set default culture info for entire c# application

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

like image 462
MonsterMMORPG Avatar asked Nov 13 '12 01:11

MonsterMMORPG


People also ask

What is default culture in C#?

NET Framework 4 and previous versions, by default, the culture of all threads is set to the Windows system culture.

How do I change the current culture in C#?

CurrentCulture = New CultureInfo("th-TH", False) Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name) ' Display the name of the current UI culture. Console. WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name) ' Change the current UI culture to ja-JP.

How do you set CultureInfo InvariantCulture?

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. ...

What is difference between CurrentCulture and CurrentUICulture?

CurrentCulture is the . NET representation of the default user locale of the system. This controls default number and date formatting and the like. CurrentUICulture refers to the default user interface language, a setting introduced in Windows 2000.


3 Answers

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"))
like image 75
Alexei Levenkov Avatar answered Oct 04 '22 02:10

Alexei Levenkov


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.

like image 27
Eric MSFT Avatar answered Oct 04 '22 02:10

Eric MSFT


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;
like image 45
Renzo Ciot Avatar answered Oct 04 '22 04:10

Renzo Ciot