Udostępnij za pośrednictwem


How to: Create Resource Files for ASP.NET Web Sites

A resource file is an XML file that can contain strings and other resources, such as image file paths. Resource files are typically used to store user interface strings that must be translated into other languages. This is because you can create a separate resource file for each language into which you want to translate a Web page.

Global resource files are available to any page or component in your Web site. Local resource files are associated with a single Web page, user control, or master page, and contain the resources for only that page. For more information, see ASP.NET Web Page Resources Overview.

In Visual Web Developer, you can use the managed resource editor to create global or local resource files. For local resource files, you can also generate a culturally neutral base resource file directly from a Web page in the designer.

Creating Resources Manually

To create a resource file manually

  1. Make sure that your Web site has a folder in which to store the resource file by doing one of the following:

    • If you are creating a global resource file, you must have a folder named App_GlobalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_GlobalResources Folder. There can only be one of these folders in an application, and it must be located at the root of the application.

    • If you are creating a local resource file, you must have a folder named App_LocalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_LocalResources Folder. There can be many of these folders in an application, and they can be located at any level in the application.

  2. To create a resource file, right-click the App_GlobalResources or App_LocalResources folder, and then click Add New Item.

    Note

    Global resource files must be in the App_GlobalResources folder. If you try to create a .resx file outside of this folder, Visual Web Developer prompts you to create it in the folder.

  3. In the Add New Item dialog box, under Visual Studio installed templates, click Assembly Resource File.

  4. In the Name box, type a name for the resource file and then click Add.

    Visual Web Developer opens the file in the Managed Resources Editor. The editor displays a grid where you can enter names (keys), values, and optional comments.

  5. Type key names and values for each resource that you need in your application, and then save the file.

    Note

    Do not attempt to embed a graphic directly in a resource file because controls will not read the resource string as a streamed image file. Resource files represent graphics by storing the URL of the graphic as a string.

  6. To create resource files for additional languages, copy the file in Solution Explorer or in Windows Explorer, and then rename it using one of the following patterns:

    • For global resource files:

      name.language.resx

      name.language-culture.resx

    • For local resource files:

      pageOrControlName.extension.language.resx

      pageOrControlName.extension.language-culture.resx

    For example, if you create a global resource file named WebResources.resx for translation to Egyptian Arabic, name the copied file WebResources.ar-eg.resx. To create a related resource file for translation to Spanish without specifying a culture, name the copied file WebResources.es.resx. Note that for local resource files, the resource file name is the page or control name, including the file name extension, and then the language and culture information.

  7. Open the copied file and translate each value, leaving the names (keys) the same.

  8. Repeat steps 6 and 7 for each additional language that you want to use.

Creating Resources From a Web Page

The following feature is not available with Visual Web Developer Express.

To generate a local resource file from an ASP.NET Web page

  1. Open the page for which you want to create a resource file.

  2. Switch to Design View.

  3. In the Tools menu, click Generate Local Resource.

    Visual Web Developer creates the App_LocalResources folder if it does not already exist. Visual Web Developer then creates the culturally neutral base resource file for the current page, which includes a key/name pair for each control property or page property that requires localization. Finally, Visual Web Developer adds a meta attribute to each ASP.NET Web server control to configure the control to use implicit localization. For more information about implicit and explicit localization, see ASP.NET Web Page Resources Overview and How to: Use Resources to Set Property Values in Web Server Controls.

  4. Type values for each resource that you need in your application, and then save the file.

    Note

    Do not attempt to embed a graphic directly in a resource file because controls will not read the resource string as a streamed image file. Resource files represent graphics by storing the URL of the graphic as a string.

  5. If the latest resource changes are not displayed, refresh Design view by switching to Source view and then switching back to Design view.

  6. Create resource files for additional languages by following steps 6 and 7 in the preceding procedure.

To generate a global resource file in Visual Web Developer

  1. In Solution Explorer, right-click the name of the Web site, click Add New Item, and then click Resource File.

    Visual Web Developer asks you if you want to place the file in the App_GlobalResources folder, and offers to create the folder.

  2. Click Yes.

  3. Type values for each resource that you need in your application, and then save the file.

    Note

    Do not attempt to embed a graphic directly in a resource file because controls will not read the resource string as a streamed image file. Resource files represent graphics by storing the URL of the graphic as a string.

  4. If the latest resource changes are not displayed, refresh Design view by switching to Source view and then switching back to Design view.

  5. To create resource files for additional languages, copy the file in Solution Explorer or in Windows Explorer, and then rename it using one of the following patterns:

    name.language.resx

    name.language-culture.resx

    Note

    When naming resource files, do not include .aspx in the name, because this can result in a naming conflict. For example, the Resource folder cannot contain two files with names such as resTestPage.resx and resTestPage.aspx.resx. In this case, the resolution of the file names at compilation would create a naming conflict, resulting in a compile-time error.

See Also

Tasks

Walkthrough: Using Resources for Localization with ASP.NET

How to: Use Resources to Set Property Values in Web Server Controls

Concepts

ASP.NET Web Page Resources Overview