Implement ASP. NET globalization

Source: Internet
Author: User
Implement ASP. NET global http://developer.51cto.com/art/200907/138761.htm

This article introduces how to implement ASP. NET globalization. We have discussed some important aspects about implementing ASP. NET globalization, and we can see that this is very easy to implement.

AD:

 

I. ASP. NET globalization information

On my website, after creating resource files and adding some localization data, I first began to use explicit localization to set the text of controls (for example, labels in my website, so that they can get their values from the resource file. Since there are four languages, I have created four resource files in addition to a fully depended resource file (without local names.

Note that these resource files use localization tags as their intermediate names. Therefore, I need to set uiculture to the same name as the localization so that ASP. NET can access these resource files.

However, the question is: How can I dynamically change the culture in the PostBack event? Fortunately, ASP. NET provides an overloaded method in the page class: initializeculture (). This method runs very early in the page lifecycle (before any controls are generated), and here we can set the uiculture and culture of the current thread.

Because this method is located in the page class and I don't want to repeat the same code for every web page, I created a basepage class, all aspx pages in my application are derived from this basepage class. But now I have another problem.

Back to uidesign: I used a masterpage and a header user control (within a contentplaceholder ). I associate a default page with this masterpage. The entire site must be dynamically localized. Therefore, there is a drop-down box at the top, from which you can select a language/culture. In the initilializeculture method of basepage, I must obtain the value of the item selected by the user from the drop-down box. However, since it has not been initialized, I cannot access the value of any control. The answer is: Use a form set (from the response object ).

Once we extract all the content from the Web application and select and use resources based on. testwebsite. xxxpropertyname has been configured with culture and uiculture, so we are ready for our global framework. Now, the only thing left is to add resource-specific data to the corresponding resource file. For each file type, we need to have a separate (and properly named) resource file. This process is called localization. In my web. config file, I use the following attributes:

  1. <GlobalizationResponseencoding "= UTF-8" requestencoding = "UTF-8" filencoding = "UTF-8"/>

2. Set the Dir attribute in the language direction

In many cases, we also need to set the direction of the localized text (this is set using the Dir attribute of the <HTML> or <body> label ). This is required because some languages read data from the right to the left (RTL), such as Arabic, which is different from the standard read mode from the left to the right (LTr) in languages such as Hindi and English. This can be achieved by setting the Dir attribute in the. resx file to an appropriate value.

First, you can create a ction (you can use any name) domain in all resource files and set its attributes to RTL or LTR based on a single resource file. For Arabic, the value of this field is RTL, while for Hindi, It is LTr. Then, set the Dir attribute of the <body> label to the following:

  1. <BodyRunat = "server" dir = "<% $ resources: testsiteresources, direction %>">

Iii. use databases for localization

We have seen how to localize the control text and UI description. But what will happen to the content stored in the database? In fact, this part of content also needs to be localized, but because it is stored in a DB, we cannot use resource files for the same purpose. To do this, we need to create a new table.

Suppose I have a table that stores user reviews. The table structure is as follows:

Now, we want to display the comments and name fields in localized text. However, we cannot store different language versions of all these domains in the same table (since there are other domains that do not need to be localized but are repeated ). Therefore, we need to reorganize the table structure and create another table to store the localized versions of the two domains. First, we need to delete these two fields from the table and create a new table as follows:

ASP. NET globalization and localization globalization

Here, we add a new domain: cultureid, which is equivalent to lcid (or locale identifier ). We can add culture-specific localized data as follows:

Now, we can use SQL queries with cultureid (lcid) as the parameter to obtain the localized content. We can also provide a user interface to input localized data into such a table so that the corresponding content can be created in an interactive manner.

Iv. Summary

In this article, we discuss some important aspects about implementing ASP. NET globalization, and we can see that this is very easy to implement.

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.