|
Going international with Locale - I
The C# Column - Yashawant Kanetkar
As
the number of Internet users grew worldwide, the need to create
software supporting multiple languages was felt intensely because
only ten percent of people use English as the primary language.
People wanted applications that could communicate with them in their
native language. This became simple with the release of the .NET
framework.
Locale and .NET culture
Geographically speaking,
a locale is a place. In software terms, a ‘locale’ is a collection
of information associated with a place. Locale information includes
the name and identifier of the spoken language, cultural conventions,
etc. A Locale Identifier (LCID) is used to retrieve information
about the locale. LCID is a 32-bit unsigned integer value that is
divided into four parts. The first two parts identify the language
and sub-language (sub-language corresponds to country/region) and
the last two parts specify the sorting order for text strings.
The ‘culture’ in .NET refers
to the user’s language and region. A culture is identified by language
code–region/country code. For example, the French language spoken
in France is identified by ‘fr-FR’. Here, ‘fr’ is the short form
for ‘French’ and ‘FR’ is the abbreviation of France. Similarly,
‘fr-CA’ is the culture identifier for French in Canada. There are
cultures that are identified by language only. These cultures are
called neutral cultures. The cultures having language-region combination
are called specific cultures. For example, ‘fr’ is a neutral culture,
whereas, ‘fr-FR’ is a specific culture.
.NET encapsulates the culture
related information in the System.Globalization and System.Resources
namespaces. The object of the Globalization. CultureInfo class represents
a specific culture. Using the methods of this class, we can obtain
the information of the culture that is set for the current thread.
The following code snippet shows how to set the culture for the
current thread and obtain its name, number format and currency format.
foreach(CultureInfo c in CultureInfo.
GetCultures(CultureTypes.AllCultures))
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(c.Name);
Console.WriteLine ( "{0}:{1},Num:{2},Dt:{ 3}, Curr:{ 4} ",c.Name,
c.EnglishName, (12345).ToString ("n"), (12345.50).ToString ("c"),
(DateTime.Now).ToShortDateString());
}
The CultureInfo.GetCultures(
) method returns the list of all the supported cultures. CultureTypes
is an enum that contains the types of culture—specific, neutral,
all cultures and only those cultures that are installed on the system.
To set the culture for a thread, firstly we must obtain the reference
to that thread. Here, we have obtained the reference by using the
CurrentThread property of the Thread class. The two culture values
of an application determine what resources are loaded for an application
and how information like currency, numbers and dates are formatted.
The resources loaded are determined by the UI culture setting, and
the formatting options are determined by the culture setting. The
Thread class provides two properties—CurrentCulture and CurrentUICulture—to
change the respective culture settings. We have used the CurrentCulture
property since we needed to set the culture specific formatting
options. The Name and EnglishName are properties of the CultureInfo
class that give the culture name and its corresponding Language
(Country/Region) combination respectively. Of course, we need to
use the Globalization and Threading namespaces to run this code.
Let’s now see how to localise
resources. We will create a localised WinForm application that allows
the user to select a language. On clicking a button another form
would get displayed in the selected language. This form would contain
a textbox where you can type your name. On clicking a button you
would be greeted in the selected language. Our form would look as
shown below.
Name the radio buttons as
reng, rger, rita, and rfre respectively. Name the ‘Display’ button
as bdisplay. Add the Click event handler for the ‘Display’ button.
Here is the handler:
void bdisplay_Click (object sender, System.EventArgs e)
{
Thread t = Thread.CurrentThread;
if(rfre.Checked)
t.CurrentUICulture=new CultureInfo("fr-FR");
if(rger.Checked)
t.CurrentUICulture=new CultureInfo("de-DE");
if(rita.Checked)
t.CurrentUICulture=new CultureInfo("it-IT");
if(reng.Checked)
t.CurrentUICulture=new CultureInfo("en-US");
greetform g=new greetform();
g.ShowDialog();
}
Here, we have only checked
which radio button the user has selected and set the UI culture
accordingly. After this we have displayed another form. We need
to add this form to the project. Add controls to this form as shown
alongside.

The controls and their names
are given in the following table:
|
Control
|
Name |
|
Name text box
|
tname |
|
Greet button
|
bgreet |
|
Label
|
lgreet |
|
Close button
|
bclose |
Add the Click event handlers
for the 'Greet' and 'Close' buttons. The bgreet_Click( ) handler
is given below:
void bgreet_Click (object sender, System.EventArgs e)
{
String s;
DateTime t=DateTime.Now;
if(t.Hour<=12)
s="Good Morning";
else
{
if( t.Hour <= 19 )
s = "Good Afternoon";
else
s = "Good Evening";
}
s += " " + tname.Text;
lgreet.Text = s;
}
This handler is simple.
We have only read the current system time and displayed a greeting
message accordingly. Before that we have retrieved the name entered
by the user in the text box and concatenated it with the message.
In the bclose_Close( ) handler
call the Dispose( ) method only to close the form.
Next time we shall see what
changes must be done to localise the greeting form.a
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanet@nagpur.dot.net.in |
|