Click here to Skip to main content
15,867,141 members
Articles / Web Development / XHTML

ASP.NET Localization (Quick Reference)

Rate me:
Please Sign up or sign in to vote.
4.91/5 (28 votes)
11 Aug 2009CPOL4 min read 183.3K   4.1K   77   20
This article gives you a quick reference about localization of the commonly used contents on an ASP.NET page, including ASP.NET server controls, HTML content, SiteMap, and other resources.

Introduction

There are a lot of articles talking about ASP.NET localization. This article is not going to give you an in-depth look of ASP.NET localization. Instead, it will give you a quick reference about localization of commonly used contents on an ASP.NET page including ASP.NET server controls, HTML content, SiteMap, and other resources.

Contents

Using the code

How to localize an ASP.NET server control?

ASP.NET server control localization is the easiest of all. Once you add a server control to your page, you can simply switch your page to “Design” mode and then go to the menu “Tools”->“Generate Local Resource”.

image002.jpg

It will generate a resource string for each ASP.NET server control on the page. In this example, a file with the name Default.aspx.resx is created. It contains all the resource name/value pairs for our Default.aspx page.

image004.jpg image005.jpg

Switch back to the source pane, and you will see it has added some code like the following in your HTML:

ASP.NET
<asp:Button ID="Button1" runat="server" Text="Hello" 
    meta:resourcekey="Button1Resource1" />

You can then copy/paste that to create a resource file for another culture. For example, you may create a Default.aspx.fr.resx for French users.

image007.jpg image008.jpg

The following screenshot is when the language is set to English in Internet Explorer.

image010.jpg

Change the language to French by going to Internet Explorer->Tools->Internet Options->Languages.

image012.jpg

Here is the French version page:

image014.jpg

How to localize HTML content?

To localize regular HTML content, you can use the <asp:Localize> control. Let’s use an example to explain it.

You have a header and a paragraph in your page.

HTML
<h1>Localization Page Header</h1>
<p>This is a demo page to show you how to do localization in ASP.NET</p>

To localize it, you need to add <asp:Localize> to them.

ASP.NET
<h1><asp:Localize ID="Header" runat="server">Localization Page Header</asp:Localize></h1>
<p><asp:Localize ID="Localize1" runat="server">This is a demo 
   page to show you how to do localization in ASP.NET</asp:Localize></p>

Then, you may manually create a resource file and add meta:resourcekey="HeaderResource1” to your page. Or, you can take advantage of Visual Studio to automatically generate it. Switch your page to “Design” mode. Go to the menu “Tools”->“Generate Local Resource”.

It will generate the following code and the resource file (i.e., yourfile.aspx.resx) for you.

Here is the code it changed:

ASP.NET
<h1><asp:Localize ID="Header" runat="server" 
   meta:resourcekey="HeaderResource1" 
   Text="Localization Page Header"></asp:Localize></h1>
<p><asp:Localize ID="Localize1" runat="server" 
   meta:resourcekey="Localize1Resource1" Text="This is a demo page to show you 
      how to do localization in ASP.NET"></asp:Localize></p>

The rest of the steps are the same as how you would localize an ASP.NET server control.

How to localize a site map?

This article will give you more details on SiteMap localization.

  1. Enable site map localization by adding enableLocalization="true" to the site map file, for example: the Web.sitemap file.
    XML
    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"
        enableLocalization="true">
  2. Change the value of the property that you want to localize to a resource string in siteMapNode using this format: “$resources:ClassName,KeyName,DefaultValue”.
    XML
    <siteMapNode url="Default.aspx" 
      title="$resources:SiteMapLocalizations,HomePageTitle" 
      description="$resources:SiteMapLocalizations,HomePageDescription">
  3. Add a global resource folder and files. Right click the web site in Solution Explorer. Click on “Add ASP.NET folder”. Click on “Add App_GlobalResources” from the sub-menu. It will add an “App_GlobalResources” folder to your root directory.

    image016.jpg

    Then, add a resource file, for example, SiteMapLocalizations.resx. In the file, you will have a name/value pair for each of the resource strings. For example:

    Name             Value
    HomePageTitle    Home
  4. Create a resource file for each culture you may have. For example, you may have a file called SiteMapLocalizations.fr.resx for French.

    Below are the screenshots of the menus in English and French:

    image018.jpg

How to localize a string programmatically?

Sometimes you may need to display a string, for example, an error message at runtime. You will need to localize it programmatically. Here is how to do it:

  1. Add a resource file under the App_GlobalResources folder. In this example, we add a file called “CommonResource.resx” and a French version “CommonResource.fr.resx”.

    image020.jpg image021.jpg

  2. Thanks Alexander Nesterenko for his comments. There are a lot of other and better ways to get the resource string.

    "You should get resources from ~\App_GlobalResources\MyMessages.resx by:

    1. Generated code wrapper -
      C#
      string message = Resources.MyMessages.Hello;
    2. Resource expression -
      ASP.NET
      <asp:Label Text="<%$ Resources: MyMessages, Hello %>" />
    3. Method GetGlobalResourceObject -
      C#
      string message = GetGlobalResourceObject("MyMessages", "Hello");

    You should get resources from ~\App_LocalResources\default.aspx.resx by:

    1. Resource expression -
      ASP.NET
      <asp:Label Text="<%$ Resources: Hello %>" />
    2. meta:resourceKey -
      ASP.NET
      <asp:Label meta:resourceKey="labelResourceKey" />
    3. Method GetLocalResourceObject -
      C#
      string message = GetLocalResourceObject("Hello"); "

Here are the screenshots:

image023.jpg

How to dynamically change culture?

In the sample code, we added two LinkButtons. When you click on “English”, the page will switch to English culture; click on “Français”, it will switch to French. To detect which link button is clicked, we use Request.Form[“__EventTarget”]. It works, but probably not in the best way.

We override the “InitializeCulture” method of the page so it will display the correct culture based on the selection we made.

HTML code:

HTML
<asp:LinkButton ID="LanguageEnglish" Text="English" runat="server"></asp:LinkButton>
<asp:LinkButton ID="LanguageFrench" Text="Français" runat="server"></asp:LinkButton>

ASP.NET code:

C#
protected override void InitializeCulture()
{
    string language = Request.Form["__EventTarget"];
    string languageId = "";

    if (!string.IsNullOrEmpty(language))
    {
        if (language.EndsWith("French"))
            languageId = "fr-FR";
        else languageId = "en-US";
        Thread.CurrentThread.CurrentCulture = 
        CultureInfo.CreateSpecificCulture(languageId);

        Thread.CurrentThread.CurrentUICulture = new CultureInfo(languageId);
    }
    base.InitializeCulture(); 
}

image025.jpg

This works for everything except the menu. If you have a menu using a SiteMap in your page, you may need to call menu.DataBind() in the Page_Load event or set EnableViewState to false. Otherwise, your menu will still show the previous culture string while other contents change to the new culture.

When we dynamically change the culture, most likely, we need to store it somewhere so it won’t get lost when we jump from page to page. In the sample application, we use Session to do that.

Happy programming!

License

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


Written By
Team Leader www.dotnetideas.com
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTranslation of Resx file. Pin
Member 1324811422-Mar-18 5:05
Member 1324811422-Mar-18 5:05 
SuggestionResX Localization Tool Pin
kryszal2-Jan-16 5:08
kryszal2-Jan-16 5:08 
QuestionIam not able to Run the file. Please help me Pin
heamnth23-Feb-14 0:26
heamnth23-Feb-14 0:26 
GeneralMy vote of 5 Pin
0ptimus Prime30-Sep-13 1:42
0ptimus Prime30-Sep-13 1:42 
QuestionBrilliant Pin
SuperNature13-Mar-13 12:38
SuperNature13-Mar-13 12:38 
GeneralExcellent Pin
sagaert30-May-12 0:56
sagaert30-May-12 0:56 
GeneralRe: Excellent Pin
DotNetIdeas30-May-12 8:16
DotNetIdeas30-May-12 8:16 
GeneralRe: Excellent Pin
sid_boss18-Jun-12 23:08
sid_boss18-Jun-12 23:08 
GeneralMy vote of 5 Pin
Ezz Khayyat21-May-12 21:53
professionalEzz Khayyat21-May-12 21:53 
GeneralMy vote of 5 Pin
smylee12-Mar-12 2:04
smylee12-Mar-12 2:04 
Questioncheck out this link Pin
Milind Karpe4-Mar-12 19:18
Milind Karpe4-Mar-12 19:18 
Generalthank you very much Pin
shakesperoo3@hotmail.com8-Jun-10 10:07
shakesperoo3@hotmail.com8-Jun-10 10:07 
GeneralWhy Pin
seb.4921-Jan-10 10:51
seb.4921-Jan-10 10:51 
GeneralRe: Why Pin
DotNetIdeas21-Jan-10 16:43
DotNetIdeas21-Jan-10 16:43 
GeneralRe: Why Pin
nmg19616-Dec-10 22:54
nmg19616-Dec-10 22:54 
GeneralRe: Why Pin
DotNetIdeas17-Dec-10 15:26
DotNetIdeas17-Dec-10 15:26 
GeneralCongratulations! Pin
Pablo Telmo1-Sep-09 9:01
Pablo Telmo1-Sep-09 9:01 
Generalresource from App_GlobalResources and App_LocalResources Pin
Alexander Nesterenko12-Aug-09 10:00
Alexander Nesterenko12-Aug-09 10:00 
GeneralThank you for your input. Pin
DotNetIdeas12-Aug-09 10:33
DotNetIdeas12-Aug-09 10:33 
GeneralRe: Thank you for your input. Pin
Alexander Nesterenko13-Aug-09 0:37
Alexander Nesterenko13-Aug-09 0:37 

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.