Microsoft KB Archive/917414

From BetaArchive Wiki

Article ID: 917414

Article Last Modified on 5/11/2007



APPLIES TO

  • Microsoft ASP.NET 2.0
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
  • Microsoft ASP.NET 1.0




INTRODUCTION

You can use application resource files (.resx) to efficiently create a localizable Microsoft ASP.NET Web application. By using resource files, you can store localized values for controls. The localized values are based on a user’s language and culture. When you store localized values in resource files, ASP.NET can select the appropriate value at run time.

A localizable ASP.NET Web application must contain the following:

  • A resource file (.resx) to store the localized values
  • Coding to associate specific controls with specific localized values

This article contains an implicit location sample that uses a combination of designer resources and editor resources to produce a resource file. This article describes how to use the resource file to create a localizable ASP.NET Web application that you can view in two languages. The method in this article uses Microsoft Visual Studio 2005 to populate the resource file by using values that are based on control properties for each page.

Note It is best to generate the resource files after you create the ASP.NET Web application and after you add controls to the page.

MORE INFORMATION

To use application resource files to efficiently create a localizable ASP.NET Web application, follow these steps.

Step 1: Prepare the application

To prepare a new ASP.NET Web application to use localized values, follow these steps:

  1. Create a Web application by using Visual Studio 2005. To do this, follow these steps:
    1. Start Visual Studio 2005.
    2. On the File menu, click Web Site.
    3. Click ASP.NET Web Site, click Visual Basic in the Language list, and then click OK.

      Note A new Web site is created, and the Default.aspx file is displayed in Source view.
    4. To switch to Design view, click Design.
  2. To display static text, add controls to the page. To do this, follow these steps:
    1. In the Toolbox, double-click the Label control to add a Label control to the page.
    2. Click Label.
    3. In the Properties window, type Date in the Text box.
    4. In the Toolbox, double-click the Label control to add a Label control to the page.
    5. Arrange this control to appear under the Date label.
    6. Click Label.
    7. In the Properties window, type Time in the Text box.
  3. To display dynamic text, add controls to the page. To do this, follow these steps:
    1. In the Toolbox, double-click the Label control to add a Label control to the page.
    2. Arrange this control to appear to the right of the Date label.
    3. In the Toolbox, double-click the Label control to add a Label control to the page.
    4. Arrange this control to appear to the right of the Time label.
    5. Double-click the page, and then add the following code to the Page_Load method.

              Label3.Text = Format(Now(), "H:mm")
              Label4.Text = Date.Now.Date

Step 2: Generate the resource files automatically

To generate the resource files automatically, follow these steps:

  1. In Solution Explorer, double-click the Default.aspx file.


Note The Default.aspx file opens in Design view.

  1. On the Tools menu, click Generate Local Resources.


Note By default, a new folder that is named App_LocalResources is created. Additionally, a resource file that is named Default.aspx.resx is created. This file contains information about each Label control on the page. The values in the resource file match the values that you entered for each Label control in Design view.

  1. In Solution Explorer, right-click the Default.aspx.resx file, and then click Copy.
  2. In Solution Explorer, right-click the App_LocalResources folder, and then click Paste.
  3. In Solution Explorer, right-click the Copy of Default.aspx.resx file, and then click Rename.
  4. Type Default.aspx.es-mx.resx, and then press ENTER.

Notes

  • Steps 3 through 6 create a localized resource file for the Spanish language. You can create a localized resource file by including the language and the culture between ".aspx" and ".resx" in the file name.
  • To edit the localized values in various resource files, open the resource files in Visual Studio 2005, and then change the properties for each localized control.

Step 3: Test the application

To test the application, follow these steps:

  1. On the Debug menu, click Start Debugging.


Note By default, Microsoft Internet Explorer starts, and the Default.aspx file of the ASP.NET Web application is displayed.

  1. On the Tools menu in Internet Explorer, click Internet Options.
  2. In the Internet Options dialog box, click the General tab, and then click Languages.
  3. In the Language Preferences dialog box, click Add.
  4. In the Add Language dialog box, click Spanish (Mexico) [es-mx], and then click OK.
  5. In the Language Preferences dialog box, click Spanish (Mexico) [es-mx], click Move Up, and then click OK.
  6. To close the Internet Options dialog box, click OK.
  7. To view the localized content on the page by using the new language settings, click Refresh on the View menu.


REFERENCES

For more information about resources in ASP.NET Web applications, visit the following Microsoft Developer Network (MSDN) Web site:

For more information about globalization, visit the following Microsoft Web site:

For more information about how to work with Web resources in ASP.NET 2.0, click the following article number to view the article in the Microsoft Knowledge Base:

910442 Working with Web resources in ASP.NET 2.0


For more information about globalization issues in Active Server Pages (ASP) and in ASP.NET, click the following article number to view the article in the Microsoft Knowledge Base:

893663 Globalization issues in ASP and ASP.NET


For more information about how to set culture information programmatically in ASP.NET, click the following article number to view the article in the Microsoft Knowledge Base:

306162 How to set current culture programmatically in an ASP.NET application


Keywords: kbhowto kbinfo KB917414