Export (0) Print
Expand All

Walkthrough: Localizing Web Forms Pages

Visual Studio .NET 2003

When you localize Web Forms pages, the Visual Studio project system does not automatically generate resource files. You must manually create and edit the XML resource files. This walkthrough topic demonstrates how to add a resource file template and then edit the template with the XML Designer to create resources in English, German, and German as spoken in Austria. This topic also discusses how to write code to access these resources.

You can also convert a text file to a resource file. For more information, see Resources in Text File Format and Resource File Generator (Resgen.exe).

To manually create and edit a resource file

  1. Create a new ASP.NET Web Application named "LocProject". For details, see Creating Web Projects.
    Note   The project must be named "LocProject"; some code later in this walkthrough relies on having that name.
  2. On the Project menu, click Add New Item.
  3. In the Templates box, select the Assembly Resource File template. Type the file name "strings.resx" in the Name box. The file strings.resx will contain fallback resources in English. These resources will be accessed whenever the application cannot find resources more appropriate to the UI culture.

    The file is added to your project in Solution Explorer and automatically opens in the XML Designer in Data view.

  4. In the Data Tables pane, select data.
  5. In the Data pane, click an empty row and enter "txtSearch" in the name column and "Search for the following text:" in the value column.

    You do not need to specify the type or mimetype for a string; they are used for objects. The type specifier holds the data type of the object being saved. The MIME type specifier holds the base type (base64) of the binary information stored, if the object consists of binary data.

  6. Click another empty row and enter "txtWelcome" in the name column and "Welcome to Localization!" in the value column.
  7. On the File menu, click Save strings.resx.
  8. Do steps 2-7 twice more to create two more resource files named "strings.de.resx" and "strings.de-AT.resx", containing the string resources specified in the following table. The file strings.de.resx will contain German resources that are neutral — that is, they apply to any region where German is spoken. The file strings.de-AT.resx will contain German resources that are specific to Austria.
    resource file namenamevalue
    strings.de.resxtxtSearchNach dem folgenden Text suchen:
    strings.de.resxtxtWelcomeWillkommen zur Lokalisierung!
    strings.de-AT.resxtxtWelcomeGrüßt's euch zur ASP.NET Lokalisierung!

    You can enter the accented characters in a variety of ways:

    • Copying and pasting from this help topic
    • Using a German keyboard layout
    • Using the character map application charmap.exe
    • Using the Windows On-Screen Keyboard in Windows 2000 and Windows XP

    For details about using a different keyboard layout, the character map application, or the on-screen keyboard, see Windows Help.

To access the resources

  1. In the Code Editor, import the System.Resources and System.Globalization namespaces at the beginning of the code module. In Visual Basic, this code should occur before the class declaration for WebForm1. In Visual C#, this code should occur before the namespace declaration. The following examples show the statements in context.
    ' Visual Basic
    Imports System.Resources
    Imports System.Globalization
    
    Public Class WebForm1
       Inherits System.Web.UI.Page
    
    // C#
    using System.Resources;
    using System.Globalization;
    
    namespace LocProject
    
  2. Declare a variable of type ResourceManager as part of the page class:
    ' Visual Basic 
    Public Class WebForm1
       Inherits System.Web.UI.Page
       Protected LocRM As ResourceManager
    
    // C# 
    namespace LocProject_CS
    {
       public class WebForm1 : System.Web.UI.Page
       {
          protected ResourceManager LocRM;
    
  3. Create an instance of the ResourceManager class by adding the following code to the Page_Load method in the WebForm1 code-behind class. The ResourceManager constructor takes two arguments. The first is the root name of the resources — that is, the name of the resource file without the culture and .resx suffixes. The second argument is the main assembly.
    ' Visual Basic 
    LocRM = New ResourceManager("LocProject.strings", GetType(WebForm1).Assembly)
    
    // C# 
    LocRM= new ResourceManager("LocProject.strings", typeof(WebForm1).Assembly);
    
    Note   If you did not name your project "LocProject," you should substitute the name you did use as part of the root name parameter.

To display a static resource string in the page

  1. In HTML view, add some static text from a resource file with the following HTML code. Place this code between the <form> and </form> tags.
    <h3><%=LocRM.GetString("txtWelcome")%></h3>
    
    Note   By default, the ResourceManager object is case-sensitive. If you wish to do case-insensitive lookups so that "TXTWELCOME" retrieves the same resource as "txtWelcome", you can set the resource manager's IgnoreCase property to true. However, for performance reasons, it is best to always specify the correct case for your resource names. Doing case-insensitive resource lookups can cause performance problems.

To assign a resource string to the property of a control

  1. In Design view, drag a Button control from the Web Forms tab of the Toolbox to your form.
  2. In the Properties window, delete the value for the button's Text property
  3. In the Code Editor, add the following code to the Page_Load method to retrieve the text for the button from the resource file.
    ' Visual Basic
    Button1.Text = LocRM.GetString("txtSearch")
    
    // C#
    Button1.Text = LocRM.GetString("txtSearch");
    
  4. In the Code Editor, add the following code to import the System.Threading namespace at the beginning of the code module.
    ' Visual Basic
    ' Import this namespace at the beginning of the code module
    Imports System.Threading
    
    // C#
    // Import this namespace at the beginning of the code module.
    using System.Threading;
    
  5. Add the following code to the very beginning of the Page_Load method to specify that the Web Forms page should display resources and formatting according to the culture specified in the browser's Accept-Language header. This code is important because otherwise your Web application will default to the culture settings of the Web server, which may not be appropriate for the remote user viewing the Web Forms page.
    ' Visual Basic
    ' Set the culture and UI culture to the browser's accept language
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))
    Thread.CurrentThread.CurrentUICulture = New CultureInfo(Request.UserLanguages(0))
    
    // C#
    // Set the culture and UI culture to the browser's accept language
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]);
    
  6. Save the project.
  7. On the Build menu, click the Build LocProject command.
  8. Right-click the WebForm1.aspx file in Solution Explorer and choose View in Browser. If you are running on a German or Austrian operating system you will see the page in German or Austrian; otherwise you will see it in English.

    If you view the page first on a system configured for German and then on a system configured for Austrian, you will notice that the Welcome text has changed to the Austrian resources, but the text for the search button has not changed. This is an example of the resource fallback system: the Austrian resources only changed the welcome text, and the remaining text defaults to the neutral German resource.

See Also

Setting the Culture and UI Culture for Web Forms Globalization | Introduction to International Applications in Visual Basic and Visual C# | Walkthrough: Localizing Windows Forms | Globalizing and Localizing Applications

Show:
© 2014 Microsoft