COM - Component Object Model
| As the number of Internet users grew worldwide, need to create software
supporting multiple languages was felt intensely because only ten percent of
people use English as primary language. People wanted an application that could
communicate with them in their native language. This became simple with the
release of .NET framework. foreach ( CultureInfo c in CultureInfo.GetCultures(
CultureTypes.AllCultures ) ) Thread.CurrentThread.CurrentCulture = new CultureInfo (
c.Name ) ; } The CultureInfo.GetCultures( ) method returns the list of
all the supported cultures. CultureTypes is an enum that contains
the types of culture viz. 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
same 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 is 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.
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 ; 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" )
; } 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 below. The controls and their names are given in the following table.
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 ; } we had designed the greeting form and added code to change the
culture. Let us now add localization support to it.
Following table shows the data to be added to German and Italian resource files.
Now you can run the program. Select a language and display the greetform. The greetform in Italian is shown in the following figure.
But there is a problem in this output. Although resources are appearing in Italian, the greeting message is still in English. This is because, when we localize resources, an entry is made in their properties to extract proper resource from appropriate resource file. Since welcome messages are string literals their properties are not changed. We have to do this job ourselves. We also need to make few changes in the program. Add the following translations in the resource files for German, French and Italian.
We must also provide default culture settings for
English. For this, we can modify the main resource file i.e. ‘greetform.resx’.
In this file we must add three resource strings and their English equivalents
that happen to be same.
ResourceManager r = new ResourceManager (
"locale_greet.greetform", s = r.GetString ( "GoodMorning" ) ; else if ( t.Hour <= 19 ) s = r.GetString ( "GoodAfternoon" ) ; else s = r.GetString ( "GoodEvening" ) ; } } The ResourceManager class provides methods
to work with culture specific resources at run-time. The
‘locale_greet.greetform’ is the root name of the resources (If resource file
name is “MyResource.en-US.resources” then root name is “MyResource”) that we
want to access from the current assembly. We have specified fully qualified name
of the resource file. We have used the ResourceManager.GetString( )
method to extract the value of the resource passed to it as parameter. Use
the System.Resources and System.Reflection namespace for the
ResourceManager and Assembly classes respectively. ResourceManager resources = new ResourceManager( typeof ( greetform ) ) ; The type greetform provided to the
constructor is used to gather all information like assembly name, root name of
resource, etc. for finding the resources. Instead of assigning values directly
to the properties, methods like GetString( ), GetObject( ) are
used to retrieve the strings and pictures from the resource file. Before
retrieving the values, the CurrentUICulture property is checked for the
current culture. |
