17.2. Providing Multiple Language Support

Problem

You want to support multiple languages in your application without developing multiple versions of each page.

Solution

Use resource files to provide the text for each user interface element in the languages you wish to support, add attributes to the static controls in your .aspx files to set their text automatically from the resource files at runtime, set the culture and uiCulture attributes of the <globalization> element in web.config to auto, and set the values for dynamic controls in the code-behind class.

Use Visual Studio 2005 to create the root resource file and add the resource attributes to the controls in your .aspx files as follows:

  1. Open the .aspx file that you want to localize.

  2. Switch to Design mode.

  3. Select Tools → Generate Local Resource from the menu. Visual Studio 2005 will add a meta:resourcekey attribute to all server controls in the file and create a root resource file with entries for each of the server controls.

  4. Duplicate the root resource file for each language and culture to be supported by your application, setting the values to the resource items as appropriate for the language.

  5. Set the culture and uiCulture attributes of the <globalization> element in web.config to auto:

    		<globalization culture="auto" uiCulture="auto" />

In the code-behind class for each page that needs to support multiple languages, use the .NET language of your choice to:

  1. Set the values of controls used for date and currency.

  2. Set the text of any controls ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.