Globalization of ASP.net 2.0 and globalization of localization

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

I. 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>
///从通用的页面头部的下拉框列表中选择的语言名。
///我们需要使用这个名字,因为我们还没有任何其它控件属性-现在控件本 身还没有被初始化。
///因此,我们使用"嵌套的"下拉框列表名,从中我们可以从Request.Form[] 集合中得到该下拉框列表的值。
/// </SUMMARY>
public const string LanguageDropDownID = "ctl00$cphHeader$Header1 $ddlLanguage";
/// <SUMMARY>
///在一个回寄表单中的PostBack事件目标域的名字。你可以使用
///它来确定是哪个控件触发了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
///that are selected by the user in the current thread. Note that this method calls
///early in the page lifecycle, and we do not currently have any controls
///, so 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[postbackeventt Arget];
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.CurrentUICu Lture = (CultureInfo) session ["Myuiculture"];
Thread.CurrentThread.CurrentCulture = (CultureInfo) session ["MyCulture"];
}
Base. InitializeCulture ();
}
///<summary>
///uses parameters to set the 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 to save 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 of 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.

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.