Globalization and Localization
Applications must be designed to accommodate users from various cultures.
Users in different parts of the world use different languages and different
formatting standards for numbers, currency, and dates. International
applications should be customizable according to the preferences of users
belonging to different nations, cultures, or regions. An application uses the
locale to identify the preference of a user. The local is the
combination of a language and a country. For example, the locale of a user
speaking English and belonging to the United States is "en-US". The locale
includes information about the formats used for representing time and date,
symbols and conventions used for representing currency, and the character
encoding scheme being used.
The structure of an internationalized application is divided into two blocks:
Code Block: Contains the application code or the executable
part of an application. This block will remain the same for all locales.
Data Block: Contains all the resources, such as text and
images used by the user interface (UI). This block is locale-specific, and
each locale will have one data block.
The process of making an application ready for customers is called
internationalization. It includes three phases:
Globalization: Involves writing the executable code for an
application in such a way that makes it culture-neutral and language-neutral.
While incorporating globalization, you must ensure that the culture-specific
details are not hard-coded into the executable code. The primary task in this
phase is to identify the various locale-sensitive resources and to isolate
these resources from the executable code.
Localizability: An application that has been globalized must
be tested to ensure that its executable code is independent of the culture and
language-specific data. This is called localizability testing. The focus of
localizability testing is to check how the application adapts itself to
different locales.
Localization: Involves the customization of an application
for a specific locale. One major task in this phase is the translation of
resources identified during the globalization phase. During this phase,
various resources, such as images and text, for the designated locale are
created.
When designing an international application, you should ensure the following
factors:
User Interface (UI): The language used to design different
UI components, such as menu, message box, static-text holder (label), is not
fixed during design phase of the application.
Information Formats: The formats of different elements, such
as currency, number, and date, are not fixed during design phase of the
application.
The different aspects of an application that should be taken into account
while incorporating localization are:
Formats: The formats used for displaying numbers, time,
dates and currency should be considered while incorporating localization.
Graphics: The locale-sensitive graphics, such as maps,
traffic signs, and calendar should be considered while incorporating
localization.
Implementing Globalization
The System.Globalization namespace provides different classes
that enable you to determine locale-specific information, such as te language
and country of an application at run time. The System.Globalization namespace
includes classes that enable you to localize your application. For example, the
Calendar class contains different calendars, such as JulianCalendar and
GrgorianCalendar.
Using the CultureInfo Class
Every locale is identified by a culture. The general format of the culture is
as follows:
<Language code>-<Country/Region code>
Language code: Represents a two-letter representation of a
language. It is always written in lowercase. For example, the language code
for Arabic is "ar" and that for Russian is "ru".
Country/Region code: Represents a two-letter representation
of a country or a region. It is always written in uppercase. For example, the
country code for Russia is "RU" and that for France is "FR".
Cultures are of two types:
Neutral culture: Signifies a culture that is linked only to a language and
not to a country. For example, "ru" is a neutral code.
Specific culture: Signifies a culture that is linked to both a country and
a language. For example, "ru-RU" is a specific code.
The following table describes some of the commonly used properties of the
CultureInfo class:
|
PROPERTY |
DESCRIPTION |
|
CurrentCulture |
Returns an instance of the CultureInfo class that
represents the culture for the current thread |
|
|
|
|
CurrentUICulture |
Returns an instance of CultureInfo class that
represents the culture for culture-specific resources. |
|
|
|
|
DisplayName |
Returns the name of the culture in format: <Language
code>-<country/region code> |
|
|
|
|
EnglishName |
Returns the English name of the culture in f<Language
code>-<country/region code> format |
Name |
Returns the name of the culture in <Language code>-<country/region code>
format |
|
LCID |
Returns the culture identifier for the instance of the CultureInfo class.
Every culture has an identifier. For e.g., 0x0407 is the identifier for
"de-DE" culture |
The following table describes some of the commonly used methods of the
CultureInfo class:
|
METHOD |
DESCRIPTION |
|
GetCultures |
Returns a list of supported cultures. |
|
|
|
|
GetFormat |
Returns an object that represents the format of a
specified type. For e.g., this method will return an object representing the
format of the date when the specified type is date/time. |
|
|
|
The following code snippet shows how to use the CultureInfo class:
CultureInfo my_CI = new CultureInfo ("ru-RU");
In the preceding code snippet, a new CultureInfo class instance by the name,
my_CI, is created that sets the culture of the instance to "ru-RU".
Working with Resources
A resource is any non-executable data that is logically deployed with an
application. A resource might be displayed in an application as an error message
or as a part of the UI. Examples of resources include the strings displayed in
the UI based on the culture settings of the system or a set of images. A
separate resource file should be created for each culture for which the
application is to be customized. Information is stored in a resource file
in the key/value format. For example, to store the welcome message you
must assign a the message to a key. Like this:
WelcomeText = "Welcome to my homepage";
In the preceding code snippet, WelcomeText is the key and "Welcome to my
homepage" is the value. There are specific naming conventions to follow when
naming resource files. The resource file for default culture should be
Strings.txt or Strings.ResX, and for any other culture
Strings.culture-name.ResX. Where Strings represents the name of the
file. For e.g., Strings.fr-CA.resx. After resource files are created in the text
form, they need to be converted into a binary format recognized by the .NET
Framework.
Generating Resource File
You can use the Resource File Generator (ResGen.exe) command prompt utility
to convert a resource file in the text format into the binary format. The syntax
is as follows:
resgen [/compile] [inputfilename.extension] [outputfilename.extension]
The extension of the input filename will be .resource.
Creating Resource-Only Assemblies
To create a resource-only assembly, perform the following steps:
Create a new Visual C# project with the Empty Project template.
From the Project menu, select the Add Existing Item option to add the
resources files to the project.
In Solution Explorer, right-click your project and select the Properties
option. The property page opens.
In the Output Type drop-down menu, change the output type of your project
to class Library. This will cause your assembly to be compiled as a .dll file.
Build you application.
The output will be a DLL file, which will contain all the resource files you
added.
Creating Satellite Assemblies
Satellite assemblies are resource-only assemblies that contain only
culture-specific resources. The name of the resources included in a satellite
assembly should follow this naming convention:
Filename.culture-subculture.resource
The name of a resource file included within a satellite assembly should
reflect the culture to which it belongs. All applications will contain one
culture-neutral main assembly, which will be loaded by default when a request is
sent to the application. The relationship between the main assembly and the
satellite assembly under the Windows platform follows a hub-and-spoke model.
When a Web application is deployed, the main assembly is always copied in the
bin folder in the application root. Satellite assemblies are copied into the
subfolders beneath the bin folder in the application root. Satellite assemblies
are copied into the subfolders beneath the bin folder based on their culture.
For example, the satellite assembly for the culture, fr, will be copied into the
bin\fr folder and that for the culture, fr-CA, will be copied into the bin\fr\CA
folder.
Perform the following steps to create a satellite assembly:
Open the Web application to which you need to add resource files.
From the project menu, select the Add existing Item option to add the
resource files to your application. Resource files should be named according
to the naming conventions.
From the Build menu, select the Build solution option to build your
solution.
Culture-specific resource files are automatically compiled into satellite
assemblies and placed in an appropriate directory structure.
The difference between resource-only and satellite assemblies is that
resource-only assemblies can contain any resource, such as images and text.
Satellite assemblies contain only culture-specific resources.
You can also use the AL.exe utility to link resource files to your
application. The AL (Assembly Linker) is a command prompt utility shipped with
the standard version of Visual Studio .NET.
Retrieving Resources at Run Time
The System.Resources namespace provides different
classes, which enable you to retrieve values from resource files. This namespace
contains the ResourceManager class which manages all
the resources associated with an application.
ResourceManager Class
It can be used to retrieve resources from an assembly. Each instance of the
ResourceManager class is associated with an assembly
that contains the resources. This class provides different constructors that can
be used according to the need. For example, when you need to retrieve resources
from an assembly, use the ResourceManager constructor, which has the following
syntax:
ResourceManager ( "BaseName" , "Assembly Name", Type)
In the preceding syntax:
BaseName: Represents the root name of the resource.
Assembly Name: Represents the name of the assembly, which
contains the resource.
Type: Represents the ResourceSet, which is the set of
resources for a specific culture. The resources associated with each culture
have a ResourceSet associated with them.
If the resources are stored in a folder instead of an assembly, you can use
the FileBasedResourceManager function of the ResourceManager
class. This function takes the following three parameters:
BaseName: Represents the root of the resource
ResourceDir: Represents the folder in which the resource is
to be searched.
Type: Represents the ResourceSet.
The other important classes in the System.Resources namespace are:
Configuring a Web application for Globalization
In ASP.NET, you can use the Web.config file to configure an application for
globalization. The <globalization> tag enables you to configure locale-specific
information, such as culture and encoding. The <globalization> tag has the
following attributes:
requestEncoding: Allows you to specify the assumed encoding of incoming
requests. The default value of this attribute is utf-8.
responseEncoding: Allows you to specify the encoding used for the response
data. The default value is utf-8.
fileEncoding: Allows you to specify the way in which the .aspx, .asmx, .asax
files are encoded in a Web application. The default value is utf-8.
culture: Allows you to specify the culture.
uiCulture: Allows you to specify the UI culture.
The following code snippet shows an example of how to use the <globalization>
tag to configure a Web application:
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="ru-RU"
uiculture="ru-RU"
/>
Practical Demonstration
Here we will be creating an ASP.NET application that is locale-aware. For
simplicity sake we our only creating it for 2 cultures, although its just the
repetition of steps that would be required if you wish to implement using more
cultures.
Our application will have an user interface with a drop down list from where the
user can select the culture of his choice and then the Text values of the
controls would be as per the selected culture.
Solution:
Perform the following steps for the same:
Create a new Web application and I have named it "globalization" as per
this example.
Change the name of the Webform1.aspx to culture.aspx.
Design the culture form. Drag two label controls, a dropdownlist and a
button.
Create two resource Files. For this right-click the project file and
select the Add->New folder. Rename this folder as Resources.
Now add new items to this project, select resource assembly file from the
template pane. Rename it as strings.resx.
In the name column write "msg" and in the value column write "Welcome to
my homepage", in this strings.resx file. Save this file and close it.
Drag this file to the "Resources" folder from the solution explorer.
Similarly add another .resx file, remember to use the culture convention
of renaming it. We will be renaming it for France culture so, the it would be
renamed as "strings.fr-FR.resx".
Now we need to generate strings.Resources file and
strings.fr-FR.resources file.For this go to the command prompt of
Visual Studio .NET (go to start->all programs->Microsoft Visual Studio .net
2003->Visual Studio .net tools->Microsoft Visual Studio .net command prompt)
Type the following command there: resgen strings.resx
Note you will need to navigate to the "Resources" folder using " cd "
command of DOS, since you have this .resx file in Resources folder.
Do this for all your .resx files and their respective .resources files
would be generated, as shown in the figure below:

You can see these files in your Resources folder of your application as shown in
the figure.
The main code performing this functionality is given below. You can download
example Visual Studio project from this link.
private
void Page_Load(object
sender, System.EventArgs e)
{
System.Resources.ResourceManager rm =ResourceManager.CreateFileBasedResourceManager( "strings", Server.MapPath("Resources")+Path.DirectorySeparatorChar
, null);
string
a = Session["lang"].ToString ();
CultureInfo cinfo=new
CultureInfo(a);
lblmsg.Text=rm.GetString
("msg",cinfo);
}
Here Session["lang"] is assigned the value of the selected item form the drop
down list, that contains the available cultures for the site.
The first output screen looks like this:
When you select any culture, the message is displayed in that particular
culture as provided by you in the .resx files (remember the name/value pairs!)
|