2. Localization of Web Application
Abstract
Localization is the processes of making an application run in multiple locations. Locations are
defined – at least in the context of .NET localization lingo – as a language and a country/region. So
typically a locale is represented by a language and country code like en-US for English in the US or fr-CA
for French in Canada. Specific regions may have some less tangible culture specific features – like right
to left text display, or specific meaning for images and icons. For example an icon in the western world
might signify some very different meaning in the eastern world.
Contents
Introduction................................................................................................................................................... 3
Illustration 1:................................................................................................................................................. 3
Step 1: Create a web application .................................................................................................... 3
Step 2: Adding resource file for base locale .................................................................................... 4
Step 3: Adding contents .................................................................................................................. 4
Step 4: Adding resource file for targeted locale.............................................................................. 5
Step 5: Using locale specific resource file......................................................................................... 6
Step 6: Creating object of ResourceManager .................................................................................. 6
Illustration 2:................................................................................................................................................. 7
Illustration 3:................................................................................................................................................. 8
Items not to miss!! ........................................................................................................................................ 9
Summary .....................................................................................................................................................10
References................................................................................................................................................... 11
Localization of Web Application 2
3. Localization of Web Application
Introduction
Resources are localizable pieces of information; strings and images typically, although anything
can really be stored as a resource. Resources are stored in a resource store of some sort and .NET by
default uses .resx resources which are stored in XML files and compiled into binary code that is
embedded in .NET assemblies. Resources are stored using resource ids which identify the resource and
let an application query for a resource. There are multiple ways of localizing a web application. Three of
them are illustrated, each of which serves different scenario improving on the previous. These three
illustrations focuses on localization of text displayed.
Illustration 1:
In this illustration the resource files are added in the Web application itself. These resource files
should be embedded resources, i.e. the build action of the same must be set to Embedded Resource as
shown in Picture #01.
Picture #01
Step 1: Create a web application
Firstly create an ASP.NET web application or a web site in Visual Studio targeting either .NET
Framework 3.0 or 3.5
Localization of Web Application 3
4. Localization of Web Application
Step 2: Adding resource file for base locale
Picture #02
Once must be careful while naming the resource file as this is an important factor for
localization (refer step 4). Let the resource file been created be named Demo.resx as shown in Picture #02.
Step 3: Adding contents
Picture #03
Once the resource file is created, make the entries as shown in Picture #03. The first column
Name represent the key to access the localized text and are case sensitive. The second column Value
holds the localized text for each locale in respective resource file. The third column Comment might
serves the purpose of having the actual text in the base version. The first and the third column remain
unchanged in the resource files of each locale. As mentioned in Introduction, resources other than text
could be sotred in resource files as shown in Picture #04.
Localization of Web Application 4
5. Localization of Web Application
Picture #04
Step 4: Adding resource file for targeted locale
Picture #05
Let us localize the web forms for International Spanish. The Language code for International
Spanish is es-ES. A resource file has to be created for each locale that is targeted. Now when we create
the resource file for the targeted locale, the File name must be Demo.es-ES.resx. (<resource-file-
name>.<language-code>.resx) as shown in Picture #05. Once done so, the base name of the resource files
is same.
Localization of Web Application 5
6. Localization of Web Application
Step 5: Using locale specific resource file
public string LocalizeThread()
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(this.Page.Request.UserLanguages[0]);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(this.Page.Request.UserLanguages[0]);
return Thread.CurrentThread.CurrentUICulture.Name;
}
The LocalizeThread() method sets the current culture of the current thread and current culture
used by the Resource Manager to look up culture-specific resources at run time. Once done, the object
of Resource Manager is created to get the localized values using the following code.
ResourceManager resources =
new ResourceManager(RESOURCESBASENAME, typeof(Part1).Assembly);
Step 6: Creating object of ResourceManager
The RESOURCEBASENAME is the root name of the resource files that the
System.Resources.ResourceManager searches for resources and can be formed as <Full Namespace>.
<Resource File Name> that can be found from the designer of the resource file. The second parameter
of the ResourceManager constructor is the main System.Reflection.Assembly for the resources. The
localized content is picked from the resource file as below.
lblCustomerName.Text = resources.GetString("Part1_lblCustomerName");
btnShowTime.Text = btnShowTime.ToolTip =
resources.GetString("Part1_btnShowTime");
Here the contents are not hard coded or embedded in the pages. Though any change of the
content would require a recompile of web application and deploy of the assembly. This is a major
setback in this illustration.
Localization of Web Application 6
7. Localization of Web Application
Illustration 2:
In this illustration we add a new project of type Class Library (Resources_One.LocalizeDemo).
This project would serve as repository of the content of each locale. The resource files creation process
is the same. Here too resource files should be embedded resources. Follow the step from 2 to 5 of
Illustration 1. The content of the resource files in the Illustration 1 and Illustration 2 will be same except
for a small change of the access modifier being set to public as shown in Picture #06.
Picture #06
Once done, the name value pairs in resource file behave as properties of the resource file class
as shown in Picture #07.
Picture #07
Add the reference of the new project to the web application. In this illustration one wouldn’t
require the creation of ResourceManager object as in step 6 of Illustration 1. The name value pairs can
be accessed as below.
lblCustomerName.Text = R1_Demo.Part2_lblCustomerName;
btnShowTime.Text = btnShowTime.ToolTip = R1_Demo.Part2_btnShowTime;
Going by this illustration, any change in the content would require deployment of only the
above class library project as the assembly that contains the localized content is all together different
unless in the previous illustration where the content files(resource files) are embedded in the web
Localization of Web Application 7
8. Localization of Web Application
application project. The next illustration solves the separate deployment issue as well as gives an added
advantage.
Illustration 3:
In this illustration we add a new project of type Class Library (Resources_Two.LocalizeDemo).
This project would also serve as repository of the content as in previous illustration. The resource files
creation process is the same. Here too resource files should be embedded resources. Follow the step
from 1 to 4 of Illustration 1. Create the resource files twice under folders “Customer1” & “Customer2” as
shown in Picture #07.
Picture #07
The step 6 in Illustration 1, where object of ResourceManager is created needs modification.
Assembly assembly =
Assembly.LoadFile(ConfigurationManager.AppSettings.Get("ResourceAsse
mblyPath") +
ConfigurationManager.AppSettings.Get("ResourceAssembly"));
ResourceManager resources = New ResourceManager(
ConfigurationManager.AppSettings.Get("ResourceBaseName"), assembly);
In this illustration assembly containing the content is loaded at runtime as shown in code piece
above. Then the resource manager object is created where this assembly is used. The purpose of having
the resource file been created twice under the folders “Customer1” and “Customer2” is to demonstrate
how to serve multiple customer with single deployment of web application. Here, customers could be
differentiated with a simple configuration; the RESOURCEBASENAME will be different for each
Localization of Web Application 8
9. Localization of Web Application
customer. As and when the customers are to be added with different content, the resource files are
replicated with new content under the new customer’s folder in the Resources_Two.LocalizeDemo
project. The purpose of replicating the resource files under each customer’s folder is to have different
RESOURCEBASENAME for each customer, which is distinctly and automatically built, once the files are
replicated under corresponding customer’s folder.
Items not to miss!!
One must definitely keep the following things in mind while localizing a web-application:
JavaScripts
Stylesheets
Images
Email Templates
Number Format
Date Format
Metric Formats
Currency Formats
Title of pages
Error Messages
Localization of Web Application 9
10. Localization of Web Application
Summary
Seeing the illustrations above one must be in a situation to judge that Localization is not an easy
process. Though a tedious process, it has to be done so that it could be used by people around the
globe, comfortably in their locale. The Illustration1 is applicable for small application where number of
pages are less as well as the contents; the frequency of update of the contents is less. As the resources
are embedded within the web site assembly, the change of the content would require a deployment of
the assembly to reflect the content change.
Coming to Illustration 2, it solves the purpose to an extent as the
resources are not embedded in web site and change of the content would require deployment of the
resource assembly only. But I personally don’t prefer the process in Illustration 2 for localization as the
Illustration 3 is all the more graceful because it serves a purpose more apart from solving the setback of
the Illustration 1. Illustration 3 serves the localization for web applications as well as serves for SaaS
(Software-as-a-Service) hosted models, where a single deployment serves multiple customers with
different contents. The different customers could be differentiated with logic and accordingly the
RESOURCEBASENAME as it will be different for different customer as shown in Picture #07 and
subsequent code piece.
Localization of Web Application 10
11. Localization of Web Application
References
http://msdn.microsoft.com/en-us/library/aa728892(VS.71).aspx
http://www.west-
wind.com/presentations/wwdbResourceProvider/introtolocalization.aspx
http://msdn.microsoft.com/en-us/library/ms379546(VS.80).aspx
http://msdn.microsoft.com/en-us/magazine/cc163609.aspx
http://en.csharp-
online.net/Localization_Like_the_Pros%E2%80%94Globalization_and_Localization_with
_ASP.NET
Localization of Web Application 11