Globalization of ASP.net 2.0 and globalization of localization

Source: Internet
Author: User
Tags case statement header locale resource switch case thread versions

asp.net 

First. Accession to globalization information


In my site, after creating a resource file and adding some localized data, I first started using explicit localization to set the text of the controls (for example, labels in my site) so that they could get their values from the resource file. Since there are four languages, I created four resource files in addition to a fully dependent resource file (no localized naming).


Note that these resource files are localized as their middle names, so I need to set UICulture to the same name as the localization so that asp.net can access these resource files.

But the question is: how can I change culture dynamically in the postback incident? Luckily, ASP. NET provides an overloaded method in the page class: InitializeCulture (). This method executes very early in the page lifecycle (before any controls are generated), and here we can set the UICulture and culture of the current thread.

Since this method is in the page class and I don't want to repeat the same code for every Web page, I created a basepage class where all the ASPX pages in my application derive from this BasePage class. But now, I'm facing another problem. Next, let me explain:

Back to UI design: I used a masterpage and a header user control (within a contentplaceholder). I have a default page associated with the masterpage. The entire site must be localized dynamically. Therefore, at the top, there is a Drop-down box from which the user can choose a language/culture. In the BasePage Initilializeculture method, I have to get the value of the item that the user selected from the Drop-down box, but because it hasn't been initialized yet, I can't access the value of any control yet. The answer is: use a collection of forms (from within the response object). Here is the implementation code:

<SUMMARY>
The language name selected from the list of Drop-down boxes for the General page header.
We need to use this name because we don't have any other control properties--now the control itself has not been initialized.
So, we use the "nested" drop-down box list name, from which we can get the value of the Drop-down list from the Request.form[collection.
</SUMMARY>
Public Const string Languagedropdownid = "Ctl00$cphheader$header1$ddllanguage";
<SUMMARY>
The name of the postback event target field in a postback form. You can use
It to determine which control is triggering the postback:
Request.form[postbackeventtarget].
</SUMMARY>
Public Const string Postbackeventtarget = "__eventtarget";

Notice how I use the "Parentcontrol:childcontrol" method to access the controls from the form collection. By using this convention, you can access any asp.net-generated nested controls. With the help of the values selected in the Form collection, I can use a switch case statement for cultural settings:

<SUMMARY>
Overload InitializeCulture method to set options selected by the user in the current thread
。 Note that this method is invoked early in the page life cycle
, and we don't currently have any controls
, you must use a form collection.
</SUMMARY>
protected override void InitializeCulture ()
{
<remarks><REMARKS>
Check if postback occurs. Cannot use IsPostBack in this method
, because this property is not set yet.
</remarks>
if (Request[postbackeventtarget]!= null) {
String ControlID = Request[postbackeventtarget];
if (Controlid.equals (Languagedropdownid)) {
String selectedvalue = Request.form[request[postbackeventtarget]]. ToString ();
Switch (SelectedValue)
{
Case "0": Setculture ("hi-in", "hi-in");
Break
Case "1": setculture ("en-us", "en-us");
Break
Case "2": Setculture ("EN-GB", "EN-GB");
Break
Case "3": Setculture ("Fr-fr", "fr-fr");
Break
Default:break;
}
}
}
<remarks>
Gets the file from the session, if it controls a new page that navigates to the same program.
</remarks>
if (session["myuiculture"]!= null && session["myculture"]!= null)
{
Thread.CurrentThread.CurrentUICulture = (CultureInfo) session["Myuiculture"];
Thread.CurrentThread.CurrentCulture = (CultureInfo) session["MyCulture"];
}
Base. InitializeCulture ();
}
<Summary>
Use parameters to set current UICulture and CurrentCulture
</Summary>
<param name= "Name" ></PARAM>
<param name= "Locale" ></PARAM>
protected void Setculture (string name, string locale) {
Thread.CurrentThread.CurrentUICulture = new CultureInfo (name);
Thread.CurrentThread.CurrentCulture = new CultureInfo (locale);
<remarks>
The user saves the culture set of the current thread in the session
So that it can be applied across pages in the current application.
</remarks>
session["myuiculture"] = Thread.CurrentThread.CurrentUICulture;
session["myculture"] = Thread.CurrentThread.CurrentCulture;
}

Therefore, the user will see this in the language he or she chooses. We need to save the file to a session or a cookie variable, because if the user moves to some other page in the same application, the culture information of the thread will be lost when the new Page class is first instantiated (HTTP is stateless!). )。 Note that if you do not want to lose the culture information of the current thread when the user's session expires, you can use cookies.

Once we extract all the content from the Web application and set up culture and UICulture based on user selection and use Resources.TestWebSite.XXXPropertyName, we are ready for our global framework. Now, the only thing left is to add resource-specific data to the appropriate resource file. For each file type, we need to have a separate (and appropriately named) resource file. This process is called localization. In my Web.config file, I used the following properties:

<globalization responseencoding "=utf-8" requestencoding= "Utf-8" fileencoding= "Utf-8"/>

Note that the encoding attribute-utf-8 (8-bit Unicode conversion format) is used here because it is a variable-length character encoding and can represent languages such as greek,arabic, in addition to its ASCII compatibility. For more information about UTF-8 encoding, please refer to this link below:

Http://en.wikipedia.org/wiki/UTF-8

In addition, it is particularly noteworthy that although we can have resource files in the original XML form at the publisher (so that users can easily edit them without having to recompile the entire site), the application will start running again if we make any changes to the resource file. This has the potential to hinder the performance of the application for this publication.

Second,  setting the corresponding Dir attribute of the language direction

In many cases, we also need to set the direction of localized text (which is set using the
First, you can create a direction (you can use any name) field in all resource files and set its properties 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:

<body runat= "Server" dir= "<%$ resources:testsiteresources, Direction%>" >

This allows you to set the correct direction because the value is from a resource file (based on the current thread culture).

third, the use of the database to achieve localization

We've seen how to localize the text and UI descriptions for a control. But what happens to the content stored in the database? In fact, this part of the content also needs localization, but because it is stored in a db, so we cannot use the resource file to achieve the same purpose. To do this, we need to create a new table.

Suppose I have a table that stores user evaluations. The table structure looks like this:


Now we want to implement localized text to display the comments and name fields, but it is not possible to store all the different language versions of these fields in the same table (since there are other domains that do not need to be localized but are duplicated). Therefore, we need to rearrange the table structure and create another table to store the localized versions of the two domains. First, we need to remove the two fields from this table and create a new table as shown below:


Here, we add a new domain: Cultureid, which is equivalent to the LCID (or locale identifier). We are able to add culturally specific localized data as follows:


Now we can use SQL queries with Cultureid (LCID) as parameters to get localized content. We also have the ability to provide a user interface to enter localized data into such a table so that it can be created in an interactive manner.
Fourth. Summary

In this article, we discussed some important aspects of implementing globalization in the development of ASP.net 2.0, and see that this is a very easy thing to achieve, but there are also many important issues to note:

1. Do not rely on the settings of your Web browser. You can display a link in your application (which can be in the head position) so that users can select their language by clicking on it.

2. Use a resource file to isolate the data in the GUI that is relevant to the description. Resource fallback is the method that ASP.net uses-when it cannot find a resource file that corresponds to a particular culture. It will first try the neutral resource file, then the default resource file or the fallback resource file (Testsiteresource.resx).

3. Use database tables to store data in a db. To do this, you need to create a separate table to store localized content.

4. If you use Sn.exe to create a strongly typed name for your main application assembly, then you need to sign your small assembly using the private key from the same pair of key pairs (generated by Sn.exe), because the assembly of the strongly typed name requires that the small assembly should also be a strongly typed name.


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.