Search form

C#.NET: Globalization

Internationalization is frequently the bane of a developer's existence. Fortunately, .NET provides a useful namespace called System.Globalization that contains most of the internationalization data you'll ever need. Simple usage of this for displaying each culture, calendar, and currency is shown here.

Obviously, the Windows command interpreter (cmd.exe) doesn't do Unicode, so running this as a console application will give you mangled formatting here for many of the languages. If you want to see them displayed properly, set up a Windows form or XAML page for viewing, or else use something like Process.Start() to show it in WordPad or similar.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace printCultures
{
    class CultureListing
    {
        static void Main(string[] args)
        {
            printCultures();
        }

        static void printCultures() {
            var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            Array.Sort(cultures, (c1, c2) => c1.Name.CompareTo(c2.Name));

            foreach (var culture in cultures)
            {
                Console.WriteLine(culture.NativeName + " " + culture.DisplayName);
                
                // Print the default calendar
                var cultureCal = culture.Calendar.ToString().Remove(0, 21).Replace("Calendar", "");
                Console.WriteLine(culture.NativeName + " " + cultureCal);
                
                /* 
                 * We have to control here for the invariant culture (abbreviated as "ivl", basically the 
                 * English language and associated with no single country/region) and neutral cultures 
                 * (no country/region or defined number or date format). RegionInfo() will throw an 
                 * ArgumentException for a neutral or invariant value.
                 */
                if (culture.ThreeLetterISOLanguageName == "ivl" || culture.IsNeutralCulture)
                {
                    continue;
                }
                else
                {
                    var cultureMoney = new RegionInfo(culture.Name);
                    Console.WriteLine(cultureMoney.CurrencySymbol + " " + cultureMoney.ISOCurrencySymbol);
                }
            }
            Console.Read();

        }

    }
}

Categories: