Dela via


Using the CurrentCulture Property

The CurrentCulture property represents current culture information on a per-thread basis. This information determines the default formats for dates, times, currency, and numbers; the sorting order of text; string comparisons; and casing. The setting made through this property is not a language setting. The property defines only data related to the standard settings for a geographical region. Therefore, your application can only set CurrentCulture to a specific culture or to the InvariantCulture. The application can use the CurrentThread property to set CurrentCulture.

Note

Changing the culture of CurrentThread requires a SecurityPermission object with the 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 culture in semi-trusted code.

Explicitly Setting the CurrentCulture Property

Your application can set the CurrentCulture property explicitly. The following code example sets the property to the specific culture German (Germany), designated "de-DE".

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

Your application must use a specific culture to initialize the CurrentCulture property. The property expects a culture that is associated with both a language and a country/region, for example, English (United States), designated "en-US". 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 your application specifies the culture "en" for neutral English, there is not a single correct setting for the date or currency format. The date might be United States format or United Kingdom format. The currency might be New Zealand format or Canadian format. If the application attempts to set the CurrentCulture property by specifying a neutral culture, an exception is thrown.

If you only have access to a neutral culture, your application can create a CultureInfo object in the format that CurrentCulture expects, using the CreateSpecificCulture method. This method maps a neutral culture to the associated default specific culture, and then creates a CultureInfo object that represents that specific culture. The following code example uses the CreateSpecificCulture method to map the neutral culture German ("de") to the specific culture German (Germany), designated "de-DE". It 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")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de");

Note

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

Explicitly Setting the CurrentCulture Property in an .aspx Page

The CreateSpecificCulture method also allows your application to use a Web browser's current language to initialize the CurrentCulture property in an .aspx page. In the following code example, the UserLanguages property retrieves the Web browser's current language as a string. The 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])
// 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

In the Windows operating system, the GetUserDefaultLCID function sets the CurrentCulture property. The user can change this property setting by changing the user culture through the regional and language options 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, have the application override the user locale defaults. The application should create a CultureInfo object using a constructor overload that accepts a useUserOverride parameter, and set this parameter to false. Using this technique causes the default settings on the user's operating system to be overridden by the .NET Framework's default settings. When formatting currency for European Union (EU) members trading in euros, your application is recommended to 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

Concepts

Formatting Numeric Data for a Specific Culture

Using the CultureInfo Class

Using the CurrentUICulture Property

Using the InvariantCulture Property

Other Resources

Resources in ASP.NET Applications