Click here to Skip to main content
15,881,687 members
Articles / Web Development / ASP.NET
Article

Using Globalization and Localization in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.65/5 (35 votes)
23 Feb 2012CPOL6 min read 205.2K   8K   78   25
How Globalization and Localization work in ASP.NET

Introduction

This article is aimed at understanding how Globalization and Localization work in ASP.NET.

Background

A few days ago, I saw a TV commercial that said that "Even the smallest business can go multinational". This is very true specially in case of software business where guys operating from their store room provide services to users on the other side of the globe.

If we need our website to be designed in such a way that it caters to the need of users from across the planet, then perhaps it is a good idea to understand and implement Globalization and Localization in our application. ASP.NET framework makes it pretty easy to implement these features by providing a lot of boilerplate functionality to the developers.

Before starting, let's get these two terms straight. Globalization is the process of designing the application in such a way that it can be used by users from across the globe (multiple cultures). Localization, on the other hand, is the process of customization that we do to have our application behave as per current culture and locale. These two things go together.

Understanding Resource Files

Imagine a scenario where we have a web page with a lot of labels and text in it. Without ASP.NET globalization mechanism in place, if I have to render these labels and text based on current locale, then I might end up having a lot of switch cases or if statements. To be precise, one case for each supported language. Now this technique could work but it is time consuming, error prone and sometimes frustrating for the developer.

To avoid having to write such code, ASP.NET provides us Resource files. The resource files are like a central repository where we can keep all the text and the web pages can simply read this resource file and get their respective labels populated. The beauty here is that if we have to support multiple languages, then we can have a separate resource file for each language (containing text in respective languages) and at run time ASP.NET engine will pick the correct resource file based on users locale and culture.

There are two types of resources:

  • Local Resources
  • Global Resources

Local resources are the resources which are specific for a page, i.e., there will be local resource file for every page. Global resources and the resources that are common for the whole website, i.e., one resource file that can be accessed by all the pages (there can be multiple global resource files too).

Using the Code

Let us now create a test web page that we will be working on to understand. We have a simple page that displays a welcome message to the user using Label control, a label indicating the current date and finally a label for displaying today's date.

globalization article image

Right now, I am populating these labels at design time, but if I have to support multiple cultures then I will have to get them from a local resource file. Visual Studio provides a mechanism for creating the local resource file from a given web page. This can be done by viewing the page in design view and then "Tools->Generate Local Resources". This will create a App_LocalResources folder and generate a local resource file for this page and start taking the text values for all the labels from that resource file. (All Local resource files reside in App_LocalResources and all the global resource files are in App_GlobalResources folder.

globalization article image

If we look at the resource file, it contains text for all the controls of the page.

globalization article image

The controls inside the page are now using the resource file to get its contents:

XML
<asp:Label ID="lblWelcome" runat="server" meta:resourcekey="lblWelcomeResource1">
</asp:Label>

Now we need to create the local resource files for this page so that the page will take the text from respective files based on current users culture. The format for having the multiple resource files for a page is: PageName.aspx.languageId-cultureId.resx. If the resource file for any laguage-culture combination is not found, then the default resource file will be taken.

If we want to display this web page in hindi-Indian, then we need a resource file Default.aspx.hi-in.resx.

globalization article image

Now if we run our page, we will not see any changes to test the changes. To test the changes, we need to set the default language of our browser to Hindi-Indian. I did that for my IE.

globalization article image

Now, when I run the page, the ASP.NET detects that user setting specifies language and region as Hindi and Indian respectively so it start pulling the text from our newly created resource file. If I do the same process for Spanish, then since the resource file for Spanish is not present, the default resource file will be taken. If we need support for Spanish, then we need to create a separate resource file for Spanish.

globalization article image

But it not a good idea to trust the users browser and system settings to get the culture specific information. It is always a good idea to provide the user an option to change the language and culture for the website. So let us provide the user with a dropdown where he can select the current language.

To accomplish this, what we need to do is to override the InitializeCulture function and set the UICulture to the user selected language.

C#
protected override void InitializeCulture()
{
    if (Request.Form["DropDownList1"] != null)
    {
        UICulture = Request.Form["DropDownList1"];
    }
    base.InitializeCulture();
}

globalization article image

This will give the user an option to switch languages dynamically. But there is one thing to notice which I circled in red. The UI is displayed in Hindi but the date format is not in Indian format. To do that, we need to set the Culture to selected culture so that the date, currency and other region specific format settings will also take effect.

C#
protected override void InitializeCulture()
{
    if (Request.Form["DropDownList1"] != null)
    {
    //for UI elements
        UICulture = Request.Form["DropDownList1"];

        //for region specific formatting
        Culture = Request.Form["DropDownList1"];
    }
    base.InitializeCulture();
}

Now we have a small web page that is capable of being displayed in English and Hindi. To sum up, it is important to understand that Resource file is the key to have globalization and localization. The Global resource files contains text that is common to all the pages in the website and any control from any page can be bound to it. Local resource file is page specific. We need separate resource file for each supported culture. It is always a good idea to have a default resource file too. Finally, we should never trust users browser settings and provide the "Language/Culture Change" option on our website too.

Points of Interest

There are few important things to be understood when we are targeting our website to multiple cultures. When we are targeting multiple cultures, we cannot assume anything about the structure and layout of web page. The reason these things are important is because translation for even small text in English could be very lengthy in other languages. So it is always a good idea to follow the standard guidelines when we implement globalization.

  • No absolute positioning of controls
  • Use fluid layout with forms
  • No absolute size of controls
  • Try to arrange controls in a table

I think, after this exercise, we can now start working on globalizing any website. It sure does take time to implement this stuff but ASP.NET relieves a lot of burden from the developer by providing some "must-have-always" functionality in place.

History

  • 23 Feb 2012: Using Globalization and Localization in ASP.NET

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions

 
PraiseSimple and Usefull Pin
khaleelsyed8-Sep-19 0:23
khaleelsyed8-Sep-19 0:23 
QuestionTranslation of Resx file. Pin
Member 1324811422-Mar-18 5:05
Member 1324811422-Mar-18 5:05 
QuestionContent page of master page Pin
Asghar Sarwary16-Oct-17 0:31
Asghar Sarwary16-Oct-17 0:31 
GeneralMy vote of 5 Pin
Minghang17-Feb-15 2:20
Minghang17-Feb-15 2:20 
GeneralMy Vote Of 4 Pin
Alireza_136213-Jan-15 16:10
Alireza_136213-Jan-15 16:10 
QuestionInitializeCulture Pin
MrJamesBound6-Nov-14 20:34
MrJamesBound6-Nov-14 20:34 
Questionproblem with uiculture in sharepoint 2010 Pin
Member 40596486-Mar-14 23:53
Member 40596486-Mar-14 23:53 
Questionhow to embeded hindi font in web application and support to all Browsers Pin
Faizymca29-Sep-13 22:40
Faizymca29-Sep-13 22:40 
BugRequest.Form["DropDownList1"] is always null Pin
dhaval.samar25-Sep-13 3:57
dhaval.samar25-Sep-13 3:57 
QuestionHow to create a globalization page without using the Internet Options to change the language? Pin
Salman Ali Shah18-Jul-13 22:33
Salman Ali Shah18-Jul-13 22:33 
Generalhow to localize default.aspx , if default.aspx is in other folder instead of root folder Pin
Rohit More3-Jul-13 20:58
Rohit More3-Jul-13 20:58 
SuggestionTranslate Dynamic Data Using Culture Pin
Member 88561791-May-13 19:46
Member 88561791-May-13 19:46 
GeneralMy vote of 4 Pin
MB Seifollahi7-Dec-12 22:51
professionalMB Seifollahi7-Dec-12 22:51 
GeneralMy vote of 5 Pin
sagar.sethi4146-Dec-12 19:43
sagar.sethi4146-Dec-12 19:43 
AnswerArticle of the Day on Microsoft's site Pin
Rahul Rajat Singh26-Nov-12 23:53
professionalRahul Rajat Singh26-Nov-12 23:53 
QuestionNot working with Chrome Pin
Sudheesh J Nair16-Oct-12 20:05
Sudheesh J Nair16-Oct-12 20:05 
QuestionI just want to set hindi font in textboxes Pin
Alok Sharma ji4-May-12 0:14
Alok Sharma ji4-May-12 0:14 
SuggestionTool For maintenance Pin
nrgjack3-May-12 3:33
professionalnrgjack3-May-12 3:33 
GeneralMy vote of 4 Pin
germ137-Apr-12 20:23
germ137-Apr-12 20:23 
Questioncheck out this link Pin
Milind Karpe4-Mar-12 19:17
Milind Karpe4-Mar-12 19:17 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey29-Feb-12 18:05
professionalManoj Kumar Choubey29-Feb-12 18:05 
GeneralRe: My vote of 5 Pin
Aman Ullah Principal Software Engineer26-Nov-12 0:40
Aman Ullah Principal Software Engineer26-Nov-12 0:40 
Great work
GeneralMy vote of 3 Pin
Helluin24-Feb-12 0:43
Helluin24-Feb-12 0:43 
GeneralRe: My vote of 3 Pin
Rui Jarimba1-Mar-12 11:00
professionalRui Jarimba1-Mar-12 11:00 
GeneralMy vote of 4 Pin
saxenaabhi623-Feb-12 19:06
saxenaabhi623-Feb-12 19:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.