List available culture codes in C#

[culture codes]

This example shows how you can make a list of the culture codes that are available on the system.

Several C# Helper examples deal with globalization including Localize a program in C#. To localize a program, you need to specify a culture code as in en-US for English in the United States or de-CH for German in Switzerland.


This example uses the following code to list the available culture codes.

// List the available culture names.
private void Form1_Load(object sender, EventArgs e)
{
    lvwCultures.FullRowSelect = true;

    // Add the names to the ListView.
    foreach (CultureInfo culture_info in
        CultureInfo.GetCultures(CultureTypes.AllCultures))
    {
        string specific_name = "(none)";
        try
        {
            specific_name = CultureInfo.CreateSpecificCulture(
                culture_info.Name).Name;
        }
        catch { }

        ListViewItem lvi =
            lvwCultures.Items.Add(culture_info.Name);
        lvi.SubItems.Add(specific_name);
        lvi.SubItems.Add(culture_info.EnglishName);
    }

    // Sort the names.
    lvwCultures.Sorting = SortOrder.Ascending;
    lvwCultures.Sort();

    // Color related cultures.
    Color color1 = Color.FromArgb(192, 255, 192);
    Color color2 = Color.LightGreen;
    Color bg_color = color2;
    string last_name = "";
    foreach (ListViewItem lvi in lvwCultures.Items)
    {
        string item_name = lvi.Text.Split('-')[0];
        if (item_name != last_name)
        {
            // Switch colors.
            bg_color = (bg_color == color1) ? color2 : color1;
            last_name = item_name;
        }
        lvi.BackColor = bg_color;
    }

    // Size the columns.
    lvwCultures.Columns[0].Width = -2;
    lvwCultures.Columns[1].Width = -2;
    lvwCultures.Columns[2].Width = -2;
}

The code starts by looping through the CultureInfo objects returned by CultureInfo.GetCultures(CultureTypes.AllCultures). For each CultureInfo object, the program adds the culture’s name, specific name, and English name to a ListView control. For some cultures the CreateSpecificCulture method may fail so the program uses a try-catch block to protect itself.

The culture codes initially come unsorted so the the program makes the ListView sort its values. It then loops through the values to give groups of related cultures the same background colors. As it loops through the values, it splits the culture’s name at the dash character. For example, the German languages have culture codes de-DE, de-AT, de-CH, and so forth. This code pulls out the de at the beginning.

If a culture’s prefix is different from the previous one, the code switches the background color it is using. In either case, the program sets the current item’s background color to the current color.

The program finishes by sizing the ListView control’s columns to fit their data.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in globalization, internationalization, system and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *