Localize ASP.NET web pages with resource files

You localize web pages so the text they display appears in the language the end user is utilizing to view your application. Do this by creating a resource file that contains the strings to display. You use a separate resource file for each target language. Then when the user changes their computer's locale via the Regional Settings Control Panel applet your application automtically displays its text in the selected language.

There are many ways to create the XML based resource files .NET uses including using the Resource File Generator (Resgen.exe) application that ships with .NET. This page, however, takes a more manual approach.

Create a new ASP.NET application then:

  1. Add a button called txtSearch.

  2. On the Project menu click Add New Item.

  3. In the Templates box select Assembly Resource File and name it strings.resx. This file will contain the default english resources to be used whenever the application cannot find resources more appropriate to the UI culture.

  4. In the Data Tables pane select data.

  5. In the Data pane in an empty row enter the name of a control on your page (ex. txtSearch) and the text Search for the following text.

  6. Save the file as strings.resx.

  7. Repeat steps 1 - 5. This time use the value Nach dem folgenden Text suchen: and save the file as strings.de.resx. Language specific characters can be entered by using the corresponding keyboard layout in the Regional Settings applet.

To access the resources add this before the declarations for WebForm1:

    '
    ' VB.NET
    '
    Imports System.Resources
    Imports System.Globalization
    Imports System.Threading

    //
    // C#
    //
    using System.Resources;
    using System.Globalization;
    using System.Threading;

Add this to the declarations at the top of the WebForm1 class:

In Visual Basic, put the code after the Inherits statement. In Visual C#, put it after the namespace declaration.

    '
    ' VB.NET
    '
    Protected LocRM As ResourceManager

    //
    // C# 
    //
    protected ResourceManager LocRM;

Add this to Page_Load method:

    '
    ' VB.NET
    '
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))
    Thread.CurrentThread.CurrentUICulture = New CultureInfo(Request.UserLanguages(0))

    LocRM = New ResourceManager("YourProject.strings", GetType(WebForm1).Assembly)
    Button1.Text = LocRM.GetString("txtSearch")

    //
    // C# 
    //
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]);

    LocRM= new ResourceManager("YourProject.strings", typeof(WebForm1).Assembly);
    Button1.Text = LocRM.GetString("txtSearch");

Build and run the project:

The form should display with the button's caption displayed in english.

Now use the Regional Settings Control Panel applet to set your Locale to Germany and rerun the application. The German text should now be displayed.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page