About BhashaIndia | Contribute | SiteMap | Register | Sign in to Windows Live ID
  Developers Patrons
Hindi Tamil Kannada Gujarati Marathi Telugu Bengali Malayalam Punjabi Konkani Oriya Sanskrit Nepali
Home > Developers > Tutorial > dotnet > CultureInfo Class Welcome Guest!

Working with CultureInfo Class

By Aparna Ravindran

Applications that are accessed across the globe should display the information with respect to each country and/or region. These applications need to be internationalized to display the information with respect to each locale. The culture information of different countries and/or regions needs to be known to internationalize these applications.
When working with applications that need to be internationalized, the cultural information of different countries and/or regions need to be known to display the information with respect to the locale. You can achieve internationalization in .NET using the CultureInfo class.
The CultureInfo class is defined in the System.Globalization namespace. This class contains culture-specific information that includes the names of the culture, languages in each culture, writing system and calendar (both Gregorian and non-Gregorian calendar). It also provides access to culture-specific objects of the DateTimeInfo class, the NumberFormat class, the TextInfo class and the CompareInfo class. These objects provide information such as formatting date and time, comparing strings, casing and sorting strings for culture-specific operations.
A culture is an identifier for a particular locale. It is defined as a combination of language and location identifiers. The format for specifying a culture includes two lowercase characters, followed by a hyphen (-), followed by two uppercase characters.
For example, hi-IN specifies Hindi in India.
 Kn-IN specifies Kannada in India.
To know the list of all available cultures, you can use the GetCultures static method of the CultureInfo class as shown below:
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
namespace Culture
{
    partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            CultureInfo[] ci;
            int NoOfCultures = 0;
            ci =
            CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo cc in ci)
            {
           if (cc.EnglishName.ToString().Contains("(India)"))
                {
                    lbFormat.Items.Add(cc.Name);
                    lbNames.Items.Add(cc.EnglishName);
                    NoOfCultures+=1;
                }
            }  
        }
    }
}
The above code lists all the available Indian Cultures. The NoOfCultures variable displays the count of the available cultures in India. The Length property displays the count of all available cultures. The Name property displays the name and language of the culture in the specified format (i.e. hi-IN) .The class also contains a property called NativeName which displays the list of all culture names in their respective languages.
The output on executing the above code is shown below:
The above figure displays the culture format and the culture name in the listboxes.
To implement the CultureInfo Class, you use the Thread class of the System.Threading namespace to handle the culture information on per-thread basis. Each thread is associated with a default CultureInfo instance, which specifies the user’s locale information. Two important properties of the System.Threading.Thread.CurrentThread are CurrentCulture and CurrentUICulture.
The CurrentCulture property informs the CLR about the format of date, time, currency, and numbers to be used. This property also informs the CLR about the rules to be used for casing, sorting, and comparing strings. You can set the value of the CurrentUICulture property to be used by the CLR to load the required resources of the user-interface.
You can use the CultureInfo class to set the CurrentCulture and the CurrentUICulture property explicitly for the current thread as shown below:
 System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi");
  System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
All culture-aware classes are culture sensitive by default. Therefore, when you set the culture of the current thread, it provides the culture-specific support for other classes such as TextInfo and CompareInfo in the System.Globalization namespace.
After you set the CultureInfo to a specific culture, you can retrieve any information related to that culture. The following code snippet shows how to display the currency specific to certain cultures.
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
namespace Culture
{
partial class Form3 : Form
{
  public Form3()
  {
   InitializeComponent();
   }
private void Form2_Load(object sender, EventArgs e)      
{
 CultureInfo[] ci;
             ci =
            CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo cc in ci)
            {
            if
            (cc.EnglishName.ToString().Contains("(India)"))
            {
                listBox1.Items.Add(cc.Name);  
            }
            }
  }
private void button1_Click(object sender, EventArgs e)
        {
           System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(listBox1.SelectedItem.ToString());
            label1.Text = "$500.00";
            label1.Text = (500).ToString("C");
   }
} //namespace
In the above code, the string format “C” is used to specify the currency format. When you set the culture to te-IN, it displays the currency in the respective format.
The output of the above code is shown below:
The above figure displays the currency format in te-IN that you chose from the list box.
The cultures are categorized into three sets as Neutral cultures, Specific cultures and Invariant cultures. Neutral Cultures are associated with the languages and not with any country and/or region. Specific Cultures are associated with a language and a country and/or region. Invariant cultures are insensitive to all the cultures. The following two lines of code are, thus, identical:
CultureInfo cultInvariant =new CultureInfo ("");
            CultureInfo cultInvariant= CultureInfo. InvariantCulture;
You can use the Invariant Cultures in situations where the results are independent of the cultures. Otherwise, it will end in cultural-inappropriate results. These cultures also follow specific hierarchy. The parent of the specific culture is the neutral culture and the parent of the neutral culture is the Invariant culture.
The Invariant Culture is used to store data, which will not be rendered directly to the users. Data stored in this will be in a known format, which will not change. Therefore, you can use this culture to store default values. Considering the above example of displaying date and time according to each culture, you can store the default format of date and time in a text file in the Invariant Culture format. To display date and time according to each culture involves retrieving data from the file and associating with the respective CultureInfo data.
Hence, the CultureInfo class provides a methodology to internationalize your applications by specifying the locale information of different countries and/or regions. Using this locale information, you can create applications in a specific language of that locale.

Partner Profile | Privacy Statement | Why Passport | Testimonials
This site uses Unicode for non-English characters and uses Open Type fonts.
©2003-2007 Microsoft Corporation. All rights reserved.