Click here to Skip to main content
15,890,897 members
Articles / Web Development / ASP.NET

Using CultureInfo for Globalization/Localization of an ASP.NET Applictation

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
8 Sep 2009CPOL3 min read 32K   8   1
Using CultureInfo for Globalization/Localization of an ASP.NET applictation.

Introduction

Globalization is the process of making an application able to handle different cultures and regions, while localization is the process of customization of an application for a specific culture and region.

You use resource files (.resx) to localize the resources in your application. You use classes in the System.Globalization namespace to make your application culture aware.

The main class in the Systme.Globalization namespace is the CultureInfo class. It contains many methods and properties to identify different cultures and configure your application to use a specific culture.

You use System.Threading.Thread.CurrentCulture and System.Threading.Thread.CurrentUICulture to configure your application to use a specific culture setting.

So how do you set which culture to use with your application?

The System.Threading.Thread.CurrentCulture property is used to set a specific culture for the application. This property mainly defines how the application will format the date time string and currency. So, if you are manipulating date time and currency in your application and want your application to correctly format them based on the user's culture, then you need to set the System.Threading.Thread.CurrentCulture property.

The System.Threading.Thread.CurrentUICulture property is used to set a neutral culture for the application. This property mainly defines what resource files to use for your application. Say, for example, for English users, you want the display content in your site in English, and for French users, you want to display your sites in French. So, you create different resource files for different languages and set this property, and your application will automatically select the correct resource file to be used based on the culture setting.

There are three types of Culture your application can use: InvariantCulture, NeutralCulture, and SpecificCulture.

  • InvariantCulture actually means culture agnostic. You use InvariantCulture when you want to compare strings, or display or compare dates in a culture agnostic way. By default, it uses the en-US culture.
  • NeutralCulture is actually a language specific culture without regard to the country specific date-time and currency formats. You use this culture to determine which language specific resource file your application will use. You can specify a NeutralCulture with two lower case characters identifying the language. For example, 'en' for English, 'fr' for French, 'es' for Spanish.
  • SpecificCulture is the language and country specific culture which determines the language to use with your application and also the date-time and currency format to use with your application. You specify the specific culture with two lower case characters identifying the language and two upper case characters identifying the country separated by hyphen. For example, 'en-US' for English (US), 'en-GB' for English (UK), 'fr-FR' for French (France) etc.

So while setting the culture to use with your application, you set the CurrentCulture property to a SpecificCulture and the CurrentUICulture property to a NeutralCulture.

Let's see an example to set the culture for an application:

C#
CultureInfo ci = new CultureInfo("en-US);
CultureInfo ciUI = new CultureInfo("en");
System.Threading.Thread.CurrentCulture = ci;
System.Threading.Thread.CurrentUICulture = ciUI;

In the above two lines, you are telling your application to use English as the culture. As said before, the CurrentCulture property will determine which format to use for displaying date-times and currency while the CurrentUICulture will determine which resource files to use while displaying strings in your application. You can see that the CurrentCulture property is set to "en-US" which a specific culture for English for United States, while the CurrentUICulture property is set to "en" which is a neutral culture.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) McAfee
India India
a 24/7 Programmer. Apart from programming, I do eat, sleep, breath for living. My areas of interests are C#, .Net Framework and Software development in general.
When I am not programming and not doing anything else(?), I play chess.

Comments and Discussions

 
QuestionHow to use Trigger function Pin
kskumaran1-Apr-14 21:17
kskumaran1-Apr-14 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.