ASP.net 2.0: How to switch between two languages by pressing a key
Source: Internet
Author: User
asp.net I want to implement a function that is to switch between two languages, such as Chinese and English, by pressing a key.
Note: I use the ASP.net Beta 2. The reference article is listed at the end.
Refer to the first article, "Implicit Localization Expressions" section. The localized content of a page is determined when all the controls are created in Frameworkinitlize (). Like what
Button1. Text = ((String)
Base. GetLocalResourceObject ("Linkbuttonresource1.text"));
So be sure to set the culture (Culture) of the page you need before frameworkinitlize ().
The second article, "Page lifecycle", lists the methods involved in the page's Lifecycle (lifecycle). The following is a list of some of the methods that start with the page's constructor (constructor) that are related to my problem.
At the beginning of the third article "The Page Lifecycle" wrote:
Once the HTTP page handler class is fully identified, the ASP.net run time calls the handler ' s ProcessRequest method to PR Ocess the request. ... (it) begins by calling the "method FrameworkInitialize", which builds the controls tree for the page. The method was a protected and virtual member of the "TemplateControl Class-the class from which Page itself derives. Any dynamically generated handler for a. aspx resource overrides FrameworkInitialize. In this method, the whole control tree for the page is built.
Next, ProcessRequest makes the page transit various phases:initialization, loading of view state information and postback data, loading of the page ' s user code and execution of postback server-side events. After this, the page enters in rendering mode:the updated view state is collected; The HTML code is generated and then sent to the output console. Finally, the page is unloaded and the request is considered completely served.
From here, I infer that frameworkinitlize () was invoked at least before Onpreinit (). But I still can't see the exact position of frameworkinitlize.
Bypassing the above mentioned. There are two ways to achieve the above requirements.
Method one. Set the culture you need in the InitializeCulture method of a page. Like what
public partial class _default:system.web.ui.page
{
protected override void InitializeCulture ()
{
String cul = session["UICulture"];
if (! String.IsNullOrEmpty (CUL))
{
Thread.CurrentThread.CurrentUICulture =
New CultureInfo (CUL);
}
}
}
But this is for a particular page. It's going to be a bit more hassle to apply to the entire site. To write this initializeculture method in a System.Web.UI.Page subclass, then let the class of all the pages inherit from the subclass rather than directly inherit System.Web.UI.Page.
Method Two. Set the culture you need in the PreRequestHandlerExecute. See online Help for the HttpApplication class:
An application executes events that are handled by modules or user code this is defined in the Global.asax file in the fol Lowing sequence:
1. beginrequest
2. AuthenticateRequest
3. Postauthenticaterequest
4. AuthorizeRequest
5. Postauthorizerequest
6. Resolverequestcache
An event handler (a page corresponding to the request URL) is created in this point.
7. Postresolverequestcache
8. Postmaprequesthandler
9. AcquireRequestState
Postacquirerequeststate
PreRequestHandlerExecute.
The event handler is executed.
PostRequestHandlerExecute
13...
I've tried beginrequest, but I can't. I think the reason is because page.request is created or assigned after this, so the culture set up in BeginRequest will be overwritten. Until 10. Postacquirerequeststate, only then be ready, only then we can set the culture that needs.
"The event handler is executed between 11 and 12." It should be the whole life cycle of the page to deal with. Method one uses InitializeCulture is here.
This will achieve the original purpose. But there is one small problem. The key event handler function will only be executed after the thread culture is set (whether with PreRequestHandlerExecute, or initializeculture). The result is that when the HTML of the page is uploaded to the browser, the culture is different from the thread culture. And when other events occur, as the culture is not changed, it will be the same as the thread culture. This behavior of the program is inconsistent, easy to confuse the user. A less-than-perfect outcome is to add the following sentence at the end of the key event handler so that the user can see the changed language immediately.
Response.Redirect (Request.path);
The drawback is that because the current page is to be regenerated, the original state (such as what the user has entered) will not be retained.
Another problem associated with this is how to localize a sitemap (Web navigation?). )。 Please refer to this article below.
ASP.net 2.0: How to localize Web navigation (sitemap) when using Web.sitemap
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