Use localized resources for ASP. NET (msdn)

Source: Internet
Author: User

An effective way to create a localized webpage is to select resources for webpage text and controls based on your language and culture. By using attributes in resource objects, ASP. NET can select the correct attributes at runtime based on the user's language and culture. This process is direct:

  • Resource file (. resx) storage value.

  • On the page, you can instruct the control to use resources for the control's property values.

  • At runtime, the property value of the control is derived from the source file.

    Note:

    In fact, you can store values in custom resource objects, such as databases. However, in this walkthrough, you will use a. resx file storage value.

Visual Web Developer allows you to generate resources for controls, control properties, and control HTML without writing any code.

The tasks described in this walkthrough include:

  • Enable ASP. NET page localization.

  • Generate a resource file and implicitly associate its value with the control in the application.

  • Create localized resource files and explicitly reference these files on the page by using declarative expressions.

Prerequisites

To complete this drill, you must:

  • Visual Web Developer (Visual Studio ).

  • . NET Framework.

Create a website

If you have already created a website in Visual Web Developer (see Walkthrough: Create a base Web page in Visual Web Developer), you can use this website and go to the next section "Using ASP.. NET for implicit localization ". Otherwise, follow the steps described below to create a new website and webpage.

Create a file system website
  1. Open Visual Web Developer.

  2. On the File menu, click New website ".

    The "New Website" dialog box appears.

  3. Under Visual Studio installed templates, click ASP. NET Website ".

  4. In the "location" box, enter the name of the folder to save the website page.

    For example, type A folder name"C: \ WebSites".

  5. In the language list, click the expected programming language.

  6. Click OK ".

    Visual Web Developer creates this folder and a new page named Default. aspx.

Implicit localization through ASP. NET

In this section, you will use implicit localization. In implicit localization, the control attributes should be automatically read from the resource file, but you do not need to explicitly specify which attributes are localized. Then, you need to create a resource file that provides localized values for specific properties. When running, ASP. NET checks controls on the page. If the control is marked as using implicit localization, ASP. NET searches the page in the resource file. If the property settings of the marked control are found, ASP. NET replaces the value in the control with the value in the resource file.

In this implicit localization example, a default resource file is generated using the designer and resource editor. You can use this file as the starting point for localization in two languages. Visual Web Developer only generates resource files based on controls on the page. Therefore, it is recommended that you regenerate the source file after completing the control layout of the page (including simple controls such as buttons, labels, and text boxes.

Place controls on the page
  1. In Solution Explorer, right-click Default. aspx, click rename, and then typeSample. aspx.

    This helps avoid confusion in other parts of the drill due to the default resource file, because the implicit resource file name uses the Page name.

  2. OpenSample. aspxSwitch to the design view.

  3. In the toolbox, setButton,LabelAnd oneTextBoxDrag the widget to the page.

  4. Set the "Text" of the preceding controlEnglish Button,English LabelAndEnglish TextBox.

After creating a page and adding a control, you can use the Visual Web Developer command to generate a local resource file for this page. The local resource file contains the resource strings of each control on the page. Visual Web Developer generates a resource string for each attribute specified in the control to be localized. The localized attributes specified for each control may be different, but most text-based attributes have been marked as localized.

Automatically generate resource files
  1. Click the designer drawing or control.

  2. Click Generate local resource in the Tools menu ".

    Visual Web Developer createsApp_LocalResourcesAnd in the folderApp_LocalResourcesCreateSample. aspx. resx. If you name the pageSample. aspxOther names, the. resx file will reflect any name you selected. When implicit localization is used, the resource file name depends on the name of each page.

  3. Switch to the "Source" view to view the changes to the control declaration.

    Visual Web Developer has added an attribute to the control to retrieve its value from the newly created resource file. For example,ButtonThe tag of the control has a newMeta: resourcekeyAttribute.

    Copy code

    <asp:Button ID="Button1" Runat="server" meta:resourcekey="ButtonResource1" Text="English Button" />

When a browser sends a request to the Web server, the request may contain information about the current language and culture. For example, a browser may send strings"En-us"To indicate that it has been set to use American English; another browser may send strings"En-gb"To indicate that it has been set to use English.

Note:

The language and regional settings in browser requests are not completely reliable for detecting your preferred language. These settings only indicate what content the browser has set to send or what content the browser has inherited from the basic operating system. In this walkthrough, you can rely on language and culture settings, but in finished applications, you should also provide users with a way to manually select language and culture.

This resource file is used as the default resource file for all requests. (It is a resource file for the rollback region .) If the browser does not specify any culture, or the browser request contains an unsupported language or culture, the resource value is extracted from the default file.

Now you have created a resource file. You can use the resource editor to put the localized text into the file.

Use resource editor to edit resource files
  1. Open Solution ExplorerSample. aspx. resx.

    Under the "value" of the resource editor, the "Text" attribute of each control placed on the page is listed. Changing the value here will change the value of the default culture.

  2. Set "ButtonResource1.Text"Edited English Text.

  3. Save the file.

Now you can test this page.

Test the page
  • Press Ctrl + F5 to run the page.

    The text provided in the resource editor is displayedButton1. When implicit localization is used, the syntax attribute in the resource file will overwrite the attribute specified on the page.

Add other regions

A combination of each language and culture requires a unique resource file. To add other regions, you can use the default file as the start point. You can create resource files for different regions and regions by creating a new resource file and including the ISO language code (suchEn-us,Fr-caAndEn-gb). These ISO code is located between the page name and the. resx file extension, such as Sample. aspx. en-us.resx. To specify a language specific to a non-regional language, delete the Country Code. For example, if you specify a French language, use the file name Sample. aspx. fr. resx.

Note:

When using the implicit localization syntax, you must create a series of separate resource files for each page.

Create a non-regional-specific French File
  1. In Solution Explorer, right-clickSample. aspx. resxFile, and click Copy ".

  2. Right-clickApp_LocalResourcesFolder, and then click paste ".

    Visual Web Developer creates a file named "Copy Sample. aspx. resx.

  3. Right-click the copy Sample. aspx. resx file, click rename, and type"Sample. aspx. fr. resx".

    Sample. aspx. fr. resxIndicates a file used for a non-regional-specific French text.

  4. OpenSample. aspx. fr. resx.

  5. SetButton,LabelAndTextBoxThe "Text" of the control is setFrench Button,French LabelAndFrench TextBox.

  6. Save the file.

Test with the standby language settings

To check whether ASP. NET is using this new resource file, you must first change the browser settings to the required culture for the request.

Modify the language settings of the browser
  1. In Microsoft Internet Explorer, Click Internet Options on the Tools menu ".

  2. Click language ".

  3. In the language preferences dialog box, click Add ".

  4. In the "language" dialog box, click "French (France) [fr]", and then click "OK ".

  5. Click Add, and then add "Spanish (Mexico) [es-mx]" to the language list.

  6. Click Add, and then add "Arabic (Egypt) [ar-eg]" to the language list.

    In the later part of this walkthrough, you will test in Spanish and Arabic.

  7. Under "language" in the "language preferences" dialog box, click "French (France) [fr]", click "Move Up", and then click "OK ".

Internet Explorer is now setFrPassed as the language settings for all requests. InSample. aspxSet the cultureAutoASP. NET tries to locate the resource file and its corresponding values based on the language and regional preferences to generate a page.

Test the page
  • In Visual Web Developer, press Ctrl + F5 to run the page.

    This page is refreshed using a localized French file instead of a value in the English version.

    Note:

    In Internet Explorer, You can reset the language settings by returning to the "language preferences" dialog box and moving the selected language in the list.

Explicit localization through ASP. NET

In the first part of this walkthrough, you have used ASP. NET implicit localization to display localized text to the control. You have generated a resource file containing the Property value, and added an Attribute for each control during this process) indicates that the control fills in its Property value (if any) from the resource file ). Implicit localization automatically works, that is, you do not need to specify the way each attribute reads information from the resource file one by one.

However, you may need to set attributes more directly. To achieve this goal, implicit localization should not be used, but explicit localization should be used. Through explicit localization, you can use an expression pointing to a resource file to set the attribute value. When the page is running, the expression is calculated, the value is read from the specified resource file, and then the attribute is set using this value.

Explicit localization is useful if a large amount of text or custom messages need to be localized in addition to controls and labels. For example, you can develop a series of localized welcome messages and thank-you messages for e-commerce websites, and then place the text on the page using an explicit declarative expression. In addition, explicit localization allows you to maintain a localized resource file set, rather than maintaining a separate file set for each page.

In this section, you will manually create resource files and reference these files using the ASP. NET declarative expression syntax. You will create a resource file for a simple thank-you message. Unlike the designer, you do not need to create a separate resource file for each ASP. NET page.

The base name of the resource file is LocalizedText. For each language to be localized, an additional file is created and the file name contains the corresponding language code (sometimes there may be regional code ). For example, for us English, a file named LocalizedText. resx is created. For Canadian French, a file named LocalizedText. fr-ca.resx is created. Both files are stored in the Resources directory of the Web application. Unlike the preceding implicit example, you do not need to maintain a resource file for each. aspx page. Instead, you can maintain a series of files for each supported language or culture.

Create a resource file
  1. In Solution Explorer, right-click the root directory of the website, click Add ASP. NET folder, and then click App_GlobalResources ".

  2. Right-click the "App_GlobalResources" folder and click "Add new project ".

  3. Under "templates installed in Visual Studio", click "resource files ".

  4. In the Name box, typeLocalizedText. resxAnd then click Add ".

    LocalizedText. resxThe file will act as a resource for the rollback culture.

  5. Create a. resx file and name itLocalizedText. fr. resx.

    String"Fr"Resource used when the file is identified as a browser language set to French (regardless of the region.

  6. Create the third. resx file and name itLocalizedText. es-mx.resx.

    String"Es-mx"Resource file used when the file is identified as a browser language set to Spanish (Mexico.

  7. OpenLocalizedText. resxFile.

  8. In the "name" column, TypeMsg1.

  9. In the first row of the value column, TypeHello .

  10. Save the file and close it.

  11. OpenLocalizedText. fr. resx, CreateMsg1Resource string, and assign a value to itBon jour. Save and close the file.

  12. OpenLocalizedText. es-mx.resx, CreateMsg1Resource string, and assign a value to itBunos días. Save and close the file.

    Note:

    If you want to insert a letter with an acute note (such as í), use the keyboard to typeAlt + 0237.

    Now it is namedMsg1The resource has three values. ASP. NET reads the value from the corresponding resource file based on the language set by the browser.

Now you have created a resource file. You can return to the page and add controls that reference the resource.

Add a Label control to the page
  1. SwitchSample. aspxSwitch to the design view.

  2. Add anotherLabelDrag the widget to the page.

  3. Right-clickLabelControl, click "properties", and then click the ellipsis (…) in the "expression" box (...) Button.

    The "expression" dialog box appears.

  4. Click text in the "binable attributes" list ".

  5. In the expression type list, select resource ".

  6. Under "expression attribute", set "ClassKey"LocalizedTextAnd set "ResourceKey"Msg1.

  7. Click OK ".

  8. Switch to the "Source" view.

    The text attribute of a tag now has an explicit expression, indicating that the base file of the resource and key to be selected will be retrieved from it.

    Copy code

    <asp:Label ID="Label2" Runat="server" Text="<%$ Resources:LocalizedText, Msg1 %>">
    Note:

    LocalizedTextThe property does not have a language indicator, regional indicator, or. resx extension because it is not the actual file name. Actually,LocalizedTextIndicates the Base Resource class. ASP. NET selects resources from files whose file name contains the corresponding language or regional code based on the Culture sent by the browser. The file name may be similarLocalizedText. fr. resxAndLocalizedText. es-mx.resxOr, if no matching language is foundLocalizedText. resx.

After you complete the resource file and add a declarative expression, you can start the test page. After the last test, the browser is set to report the French language preferences. During the test, the browser language is changed multiple times.

Test the page
  1. Press Ctrl + F5 to run the page.

    The French text provided in the resource editor is displayedLabelControl text.

  2. In Microsoft Internet Explorer, Click Internet Options on the Tools menu ".

  3. Click language ".

  4. In the language preferences dialog box, move "Spanish (Mexico) [es-mx]" to the top of the language list. Click OK and close the Internet Options dialog box.

  5. Press F5 to refresh the browser.

    The Spanish version of the text is displayed.

  6. Change the language to Arabic and press F5 again to refresh the page.

    In this case, the text is displayed in the language used in the resource file rollback. Because no file is createdLocalizedText. ar-eg.resxASP. NET cannot locate the text that matches the language and culture of the browser report, so it uses the rollback resource file.

  7. After testing the page, set the language as the preferred language again.

  8. Http://msdn.microsoft.com/zh-cn/library/fw69ke6f (VS.80). aspx

  9. (Global) Note: if you do not want to display it in the preferred language set by the browser, it can also be displayed on the web. in config, the settings are displayed in the form of a certain language and region: <globalization culture = "es-mx" uiCulture = "fr"/>, in this example, the culture is the set language (such as Chinese (China) [zh-cn]), and The uiCulture is used to set the resource items in the App_GlobalResources to be applied. for example, LocalizedText. fr. resx is written as uiCulture = "fr ")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.