Export (0) Print
Expand All

Using the CurrentCulture Property 

The CultureInfo.CurrentCulture property is a per-thread setting that determines the default formats for dates, times, currency, and numbers, the sorting order of text, string comparisons, and casing. The CurrentCulture property is not a language setting. It contains only data related to the standard settings for a geographical region. Therefore, the CurrentCulture property can only be set to a specific culture or to the InvariantCulture. You can use the Thread.CurrentThread property to set CurrentCulture. For more information, see the examples provided later in this topic.

NoteNote

Changing the culture of Thread.CurrentThread requires a SecurityPermission with the SecurityPermissionFlag,ControlThread set. Manipulating threads is dangerous because of the security state associated with threads. Therefore, this permission should be given only to trustworthy code, and then only as necessary. You cannot change a thread's culture in semi-trusted code.

Explicitly Setting the CurrentCulture Property

You can set the CurrentCulture property explicitly in code. The following code example sets the CurrentCulture property to the specific culture "de-DE" for German in Germany.

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");

You must use a specific culture to initialize the CurrentCulture property. The CurrentCulture property expects a culture that is associated with both a language and a region, such as ("en-US") for English in the United States. Because a language is often spoken in more than one country or region, the regional information is necessary to determine the appropriate formatting conventions to use. For example, if you specify the culture "en" for neutral English, there is not a single correct setting for the date or currency format. The date could be U.S. format or British format. The currency could be New Zealand format or Canadian format. Because of these ambiguities it is necessary to specify a specific culture such as "en-US", "en-GB" or "en-CA". If you attempt to set the CurrentCulture property by specifying a neutral culture, an exception will be thrown.

If you only have access to a neutral culture, you can create a CultureInfo object in the format that the CurrentCulture property expects, using the CultureInfo.CreateSpecificCulture method. This method maps a neutral culture to the default specific culture it is associated with, and then creates a CultureInfo object that represents that specific culture. The following code example uses the CultureInfo.CreateSpecificCulture method to map the neutral culture "de" to the specific culture "de-DE", then creates a CultureInfo object for "de-DE" and uses it to initialize the value of the CurrentCulture property.

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de");
NoteNote

This method is optional. If the mapping it provides is not appropriate for your application, you can substitute your own mapping.

Explicitly Setting the CurrentCulture Property in an .aspx Page

The CultureInfo.CreateSpecificCulture method also allows you to use a Web browser's current language to initialize the CurrentCulture property in an .aspx page.

In the following code example, the Request.UserLanguages property returns the Web browser's current language as a string. The CultureInfo.CreateSpecificCulture method parses this string and returns a CultureInfo object in the format that can be used to initialize the value of the CurrentCulture property.

// Sets the CurrentCulture property to the culture associated with the Web
// browser's current language setting.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);

For more information about using and retrieving resources in ASP.NET applications, see Resources in ASP.NET Applications.

Implicitly Setting the CurrentCulture Property

The CurrentCulture property is set by the GetUserDefaultLCID function in the Windows operating system. The user can change this property setting by changing the User Locale through the Regional Options dialog box in Control Panel or by changing settings related to user locale, such as currency, number, date, and time formats.

If you want to ensure that your application uses the default formats provided by the .NET Framework for currency, numbers, date, and time for a specified culture, you can override the User Locale defaults in your application's code. Create a CultureInfo object using one of its constructor overloads that accepts a useUserOverride parameter and set this parameter to false. This will cause the default settings on the user's system to be overridden by the .NET Framework's default settings. When formatting currency for European Union (EU) members trading in euros, it is recommended that you set the useUserOverride parameter to false to ensure that the correct currency symbol is used. For more information, see the "Formatting Currency for Euro Nations" subtopic in the Formatting Numeric Data for a Specific Culture topic.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft