Use application resource files (.resx) to efficiently create a localizable ASP.NET Web application
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 to populate the resource file by using values that are based on control properties for each page.
Original product version: ASP.NET
Original KB number: 917414
Summary
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 ones:
- 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.
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.
Step 1: Prepare the application
To prepare a new ASP.NET Web application to use localized values, follow these steps:
Create a Web application by using Visual Studio. Follow these steps:
Start Visual Studio.
On the File menu, select Web Site.
Select ASP.NET Web Site, select Visual Basic in the Language list, and then select OK.
Note
A new Web site is created, and the Default.aspx file is displayed in Source view.
To switch to Design view, select Design.
To display static text, add controls to the page. Follow these steps:
- In the Toolbox, double-click the Label control to add a Label control to the page.
- Select Label.
- In the Properties window, type Date in the Text box.
- In the Toolbox, double-click the Label control to add a Label control to the page.
- Arrange this control to appear under the Date label.
- Select Label.
- In the Properties window, type Time in the Text box.
To display dynamic text, add controls to the page. Follow these steps:
In the Toolbox, double-click the Label control to add a Label control to the page.
Arrange this control to appear to the right of the Date label.
In the Toolbox, double-click the Label control to add a Label control to the page.
Arrange this control to appear to the right of the Time label.
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:
In Solution Explorer, double-click the Default.aspx file.
Note
The Default.aspx file opens in Design view.
On the Tools menu, select 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.
In Solution Explorer, right-click the Default.aspx.resx file, and then select Copy.
In Solution Explorer, right-click the App_LocalResources folder, and then select Paste.
In Solution Explorer, right-click the Copy of Default.aspx.resx file, and then select Rename.
Type Default.aspx.es-mx.resx, and then press ENTER.
Note
- 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, and then change the properties for each localized control.
Step 3: Test the application
To test the application, follow these steps:
On the Debug menu, select Start Debugging.
Note
By default, Microsoft Internet Explorer starts, and the Default.aspx file of the ASP.NET web application is displayed.
On the Tools menu in Internet Explorer, select Internet Options.
In the Internet Options dialog box, select the General tab, and then select Languages.
In the Language Preferences dialog box, select Add.
In the Add Language dialog box, select Spanish (Mexico) [es-mx], and then click OK.
In the Language Preferences dialog box, select Spanish (Mexico) [es-mx], select Move Up, and then select OK.
To close the Internet Options dialog box, select OK.
To view the localized content on the page by using the new language settings, select Refresh on the View menu.