Thursday, January 13, 2005

Checking for presence of Indic support from .NET

The CultureInfo class under the System.Globalization namespace is indispensable when writing applications that require Globalization/Localization support. To create an instance of this class, you simply pass the name of the culture; e.g. to create an instance of hi-IN culture, I will do something like this:


CultureInfo ciHindi = new CultureInfo("hi-IN");


There are other CultureInfo constructors too for example there is one which takes the LCID of the culture you are interested in. So the above snippet could also be written as:


CultureInfo ciHindi = new CultureInfo(1081);


On English version of Windows - one most widespread in India - Indic and Far-Eastern language support needs to be installed separately from Control Panel:



If you do not have Indic support enabled, the user might get issues when displaying Indian language characters (e.g. trying to display CultureInfo.LocalName will simply show you a string of boxes). It might be a good idea to prompt the user to enable Indic support prior to running your application. Here is how you can check for its presence on a client machine:


using System;
using System.Globalization;

public class SamplesCultureInfo
{
    public static void Main()
    {
        bool bIndicInstalled = false;
        // Displays several properties of the neutral cultures.
        foreach ( CultureInfo ci in CultureInfo.GetCultures( CultureTypes.InstalledWin32Cultures ) )
        {
            if(ci.LCID == 1081) //hi-IN - since all Indic cultures are installed en-masse,
                     //presence of one indicates presence of others too
            {
                bIndicInstalled = true;
                break;
            }
        }

        if(bIndicInstalled)
        {
            Console.WriteLine("Indic support is present on this machine");
        }
        else
        {
            Console.WriteLine("Indic support absent from this machine");
        }
    }
}

2 comments:

Anonymous said...

Wouldnt it be easier to just create an indic locale and catch the exception if it is not installed (Just a guess, as I dont have a machine without indic locale), instead of looping through available cultures.

Anand
http://www.dotnetindia.com

Deepak said...

Hi Anand,

Nope creating CultureInfo for hi-IN (or any other Indic culture for that matter) won't give you an exception. Even if it did its a bad idea to use exceptions for controlling flow of your program... (just like it is a good idea to use File.Exists to check for a file rather than depending on the exception)

Cheers,
Deepak