 |
Introduction
Creating applications that are ready for the international market has
become a key tenant in any application today. The need is to
create applications that can cater to various different countries,
cultures as well as reduce the cost of developing and debugging
separate applications for each country. Creating applications that are
ready to cater to different countries of the world is called as
Internationalization.
Internationalization has two separate issues, which are often
very confusing for developers to understand. These issues are
Globalization and Localization.
In layman’s terms :
Globalization refers to creating applications where the
currency format, date format, numerical system might differ. For
example, the currency symbol for USA Dollar is $ while the currency
symbol for UK Pound is £, hence your application supporting
globalization should be able to automatically pickup the correct
format from the users operating system and use it.
Localization refers to creating applications where the screen
text, strings, pictures might differ from language to language.
(Please note I am not referring to programming languages here, but
spoken languages! ) In this short article I will cover the creation of a simple
“Localized” Windows Forms application. So as you might have already
guessed, it will deal with creating a Windows Forms application that
displays differently in different languages.
Since my knowledge of foreign languages except English is Zero, I will
be working with Hindi (I am not good at typing this too..) and English
!!
Prerequisites
1) VS.NET v7.0.9466 (.NET SDK v1.0.3705)
2) Windows 2000 (I guess admin access might be required..).
And please backup your stuff!!
3) Basic understanding of C# and Windows Forms…
4) If you do not have the necessary font's installed on your computer,
parts of this article may look garbled! Only way out is to install the
necessary fonts.
Setting-up Windows 2000 to support Hindi Language
One of the good features of Windows 2000 is that you can work with
multiple languages from within the same OS. But by default not all
languages get installed (to save space and startup time..) so you will
have to first install the Hindi language fonts if they are not already
installed.
Go to Start menu -> Control Panel -> Regional Options to start the
Regional Options dialog (Figure 1).

Figure 1: Regional Options Dialog
In the Regional Options dialog's General tab, check if the
Indic language
is selected; if not, select it and click OK. This will install the
necessary Indian fonts for Hindi, Marathi, Gujarati, Konkani, Tamil
etc. This step might ask you for the Windows 2000 Installation CD, so
keep it handy, and it might also ask for a reboot.
Once the machine reboots and the necessary font’s are installed again
start the same Regional Options dialog, and go to Input
Locales tab and
click Change. The Text Services dialog that shows, will allow you to
select keyboard settings to use. Click Add to add a new Keyboard
Layout, from the Add Input Language dialog, select Hindi and click
OK.

Figure 2: Text Services Dialog Figure 2, shows the Text Services dialog updated after I have added
the Hindi keyboard. Here you can setup the Language Bar, and Key
Settings so that you can easily shift between Hindi and English
keyboards, but this is clearly out of the scope of this article, so
explore this stuff yourself.
Also you will find that the Default Input Language dialog let’s you
select the default language for your keyboard. Later in this article I
will ask you to shift from English to Hindi keyboard layout’s, this is
the place you do that, I will not be mentioning it again!!
How does .NET Handle Localization??
.NET applications rely on Resource files (*.resx) to deal with
Localization. The logic is simple, every bit of resource like label
string, menu string, pictures, error messages, control size and
location, etc are stored in these XML formatted resource files. Hence
for each different language your application will support you create a
new resource file and place the relevant translation (translation to
be done by a human translator) of the strings and pictures into the
resource files. At runtime the appropriate resource file is selected
and the resources are used from it to display the necessary UI, making
your single application localized.
.NET follows a consistent naming pattern for resource files so that
the correct resource file is selected. The default resource file is
saved as <ClassName>.resx. (Where ClassName is the name of the class
that host’s the Main method to start the application.) Now for other
languages you have to utilize the Culture Name for that language. MSDN
documentation states that, “The culture names follow the RFC 1766
standard in the format "<languagecode2>-<country/regioncode2>", where
<languagecode2> is a lowercase two-letter code derived from ISO 639-1
and <country/regioncode2> is an uppercase two-letter code derived from
ISO 3166.”
Hence our Hindi resource file will be named <ClassName>.hi.resX.
(Where hi is the culture name for Hindi ).
If you have the VS.NET Documentation installed check out the
following link to get a full list of Culture Names.
[ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemGlobalizationCultureInfoClassTopic.htm
]
Let’s Kick some Code!
Finally, we start writing our application!! I am assuming that you
are familiar with VS.NET, and so I will not be describing small little
steps… The source code is attached, you can refer to it if you have
doubts!
1) Start VS.Net and create a new Windows Application project, call it
as LocalizationDemo.
2) Drag 1 GroupBox, 2 RadioButtons and 1 Button on to the Designer,
and make the Form look like figure 3.

Figure 3: Form Design
4) Now, right-click the LocalizationDemo Project in the Solution
Explorer and add a New Windows Form with the name Form2.cs.
5) Add a Label, PictureBox and a Button on Form2 as shown in figure 4.
I have set the PictureBox to show a custom bitmap (English.bmp) I have created.

Figure 4: Form2 Designer
6) Double-click the Close button in the Designer window so that we can
write the EventHandler for the Click event of the button. In the event
handler, we write nothing fancy but just a simply call the ‘Hide’
method on the form, as shown below.
7) Switch back to the Form2 Designer window and select the form in the
Designer; and from the Properties window set the Localizable
property
of this form to true, indicating that this form supports Localization.
8) While Form2 is selected in the Designer window, from the Properties
window, change the Language property from (default) to Hindi. This
will prompt VS.Net to add another Resource file in the project with
the name Form2.hi.resx. (I hope you remember why the particular naming
style is adopted !!).
Please remember that all the changes that you make from here on to the
components property will only get reflected while the user is viewing
the application with the culture Hindi. In case you have added some
new controls, you will have to switch back to the Default Language
property of the Form and again make the changes in the default
language also. This way you have to keep switching between the
different languages you support in you applications and make the
necessary UI changes to support all the different languages your
application supports.
9) Now time to code in Hindi!! Go to Regional Options and change the
default Keyboard to Hindi (I hope you remember how to do this!!).
10) Many times on English version of VS.NET source code files are
saved in Western European (Windows) - Codepage 1252 format,
hence if you try to directly enter Unicode Characters in Properties
window it does not work correctly. Hence switch to code view
for Form2 and then from File menu -> Advanced Save Options
dialog change the Encoding to Unicode (UTF-8 with Signature)
- Codepage 65001 and click OK. This step will ensure that
in future the source code file is saved in UTF-8 format.
11) Switch back to Design view and change the Text property of the Label to reflect नमस्ते (Hello
in Hindi) and also change the Font to Mangal. Then change the
Text
property of the button to बंद (Close in Hindi). Lastly, I have also
changed to PictureBox to show another picture. The changes shown look
like figure 5.

Figure 5: Hindi translation of Form2
And if you are wondering how I wrote नमस्ते and बंद, well with a
little bit of practice you too will get away with it, but I will give
it out this time, use the following keystrokes to make the word:
नमस्ते - vcmdls
बंद – yxo
12) Go back to the Regional Options dialog and re-set the default
keyboard to English so that we can resume with our coding in English!
If you forget this step, then you might start getting varying response
from application that are not designed to support Localization!! You
might even have to restart VS.NET, if it still keeps typing in Hindi!
13) Close the Form2 designer, and open the Form1 in the VS.NET
designer window. Once the Form1 is open in the Designer window,
double-click on the Start Application button so that we can write the
Event Handler for the Click event of the button.
14) The code snip below is the EventHandler of the Start Application
button’s Click event. Here, depending on which RadioButton is checked,
I create an instance of the CultureInfo class and assign it to the
CurrentUICulture property of the current thread.
This will make the necessary switch between the different UI’s.
private void button1_Click(object sender, System.EventArgs e)
{ if( this.radioButton1.Checked ) { //Get the Default installed culture System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InstalledUICulture ; } else if( this.radioButton2.Checked ) { //Get the Hindi culture System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi") ; } Form2 f2 = new Form2(); f2.Show();
} |
14) That’s It !! Build your code and run it, if you followed the steps
correctly the application should build and work correctly!!
Some Improvements
Here is a list of improvements you could make,
1) Currently, I am asking the user to give the language preference
every time he runs the application. It would be a better design if you
would ask for the user’s language preference only once at install
time, and then store the language preference into the application
configuration file.
2) This applications takes it for granted that the default language of
the user is English, in your application you should not make this
assumption, instead you should build an additional English (en)
resource file separately an not rely on the default resource file.
Conclusion
.NET has provided an extensive platform that support
Internationalization. VS.NET harnesses this and helps you easily
create applications supporting Internationalizations.
This article showed you one way of working with Localization through
VS.NET. |
 |