In common: localization of ASP. NET

Source: Internet
Author: User

In the actual development process, we often encounter this situation: the same website needs to display different languages in different countries. English is displayed in the United States and Chinese is displayed in China. The following uses a few simple examples to illustrate the Localization support provided by ASP. NET.

Wedge:

ASP. there are two types of resource files in. Net: one is a global resource file, which is placed in the app_globalresources directory and can be accessed by any page or site on the site; the other is a local resource file, stored in the app_localresources directory, it can only be accessed by a specific page.
The two resource files are displayed in the following example.

 

1.Apply localization to server controls.

Scenario:

 There is a button on the page that allows it to display "read me" in the United States and "read me" in China"

Implementation:

1.1. Drag a button to the page and set its text attribute.


      1.2. Select the button and select "Tools"-> "generate local resource" from the menu ".

      (Because I use an English system, if you use a Chinese system, find the Chinese menu at the corresponding location)

Click the app_localresources folder and default. aspx. resx are created.

The default. aspx. resx created here is a local resource file and can only be found in the default. aspxCode.

Open default. aspx. resx and you can see that some key-value pairs related to our button have been automatically created.

Switch default. aspx to the code mode and find that the resource-related attributes are automatically added.

1.3. Now copy default. aspx. resx and rename it default. aspx. zh-CN.resx.


1.4. Open default. aspx. zh-CN.resx and change the value of button1resource1. Text to Chinese.


Running result:

Browsing this page in IE, because I use an English system, the default display is read me.

Now let's change the IE language. In the IE menu, choose tools> Internet Options> languages, and move Chinese to the first place.

When I browsed this page again, I found that the text of the button had changed to Chinese.

 

2.Localized HTML content.

Scenario:

In the above example, we have seen how to localize the server control. If there is a static HTML content, how can we localize it?

For example, in the following HTML code, I click tools> generate local resource without any response.


Implementation:

2.1. Pack the static HTML code with the ASP: localize control. Note that the ASP: localize control must have a unique ID and runat = "server" attribute.


2.2. Select the ASP: localize control in the design view, and click tools> generate local resource in the menu. The labels in the Code view are changed.


2.3 double-click default. aspx. resx and you will find that the corresponding key-value pair has been added.


2.4. of course, for default. aspx. zh-CN.resx, We need to manually add the same key-value pair.


Running result:

The following are the execution results when the IE language is set to English and Chinese:



 

3.Format site map.

Scenario:

The website has a site navigation implemented by using site map. Now we want to make it also localized.

The following is the code structure:


 


 
 

Implementation:

3.1. Create two resource files for site map.

Because site map is used globally and its corresponding resource files are also globally valid, these two resource files must be placed in the app_globalresources folder instead of app_localresources. OtherwiseProgramAn error is reported.

3.2. Add English and Chinese key-value pairs to the two resource files respectively.

3.3. Add localization-related attributes for Web. sitemap.

 

Specific attributes include:

3.3.1. Add enablelocalization = "true" to the root node of sitemap"

3.3.2. For strings that require local output, point to the resource file in the form of $ resources: classname, keyname, and defaultvalue. In our example, the default value is not specified. If you want to specify the default value, add a comma (,) and then write the value.

Running result:

The following are the display results in English and Chinese:

 

4.Several Methods for getting resource file key-value pairs;

Question:

I created a resource file and defined some key-value pairs in it. Which of the following methods can I obtain the key-value pairs in the resource file?


Answer:

There are several methods:

4.1. read Global Resources

4.1.1.


4.1.2.


4.1.3.


4.2 read local resources.

4.2.1.


4.2.2.


4.2.3.


 

5. dynamically change the current language.

      Scenario:

      In the above examples, when we test the page, we manually change the IE settings to select the language. So how can we do this through a program?

      Implementation:

      The following is an example.

      5.1. Create a ListBox and a button to pass the language selected by the user to the server.


      5.2. Rewrite the initializeculture method on the server side to obtain the user selected and set the current display language.


      Running result:

      When selecting Chinese:


      When selecting English:

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.