I'm not sure if I should post this here. What I want to do
involves some new localization features provided by ASP.NET 2.0.
But my problem is quite generic even for ASP.NET 1.x. In a shor
word, where can I set/change the page's culture prior to
FrameworkInitialize()?
I am using ASP.NET 2.0 Beta 2. I want to toggle the page between two
languages by clicking a toggle button on the page. In the
button's click event handler, I will toggle the language which is saved
inside a session variable. I try to render the page using the new
language, but it does not work.
I have read three articles about the improved localization for ASP.NET
2.0, and the page lifecycle/pipeline. They are listed at the end
of this post.
From article 1 - section "Implicit Localization Expressions", I know
that the localized content of the page is decided by the following code
invoked inside FrameworkInitlize().
button1.Text = ((string)
base.GetLocalResourceObject("LinkButtonResource1.Text"));
Then I think I should set the page's culture before the execution of
FrameworkInitlize(). But I'm not sure of the exact place in the
page lifecycle.
In article 2 - section "Page Lifecycle", there are a list of Page
Lifecycle methods. The following is a partial list at the
beginning of the lifecycle that I think are relevant with my problem.
Constructor
Construct
TestDeviceFilter
AddParsedSubObject
DeterminePostBackMode
OnPreInit
LoadPersonalizationData
InitializeThemes
OnInit
In article 3 - section "The Page Lifecycle", it says:
Once the HTTP page handler class is fully identified, the ASP.NET run
time calls the handler's ProcessRequest method to process the request.
... (it) begins by calling the method FrameworkInitialize, which builds
the controls tree for the page. The method is a protected and virtual
member of the TemplateControl class - the class from which Page itself
derives. Any dynamically generated handler for an .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 that, 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 this, I deduce that the method FrameworkInitlize() is called at
least prior to OnPreInit(). But I do not know where its exact
position is in the lifecycle.
Anyway, I tried the following.
I override the method Construct to toggle the current thread's culture,
as shown below. But it does not work. The displayed page is
still in the browser's language.
protected override void Construct()
{
base.Construct();
string l;
if(Thread.CurrentThread.CurrentUICulture.Name == "en-US")
{
l = "zh-CN";
}
else
{
l = "en-US";
}
CultureInfo ci = CultureInfo.CreateSpecificCulture(l);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
I also tried to overried OnInit() and OnPreInit() to set the page's culture, but it is still futile.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.Culture = Language;
}
I'll be greatly appreciated if anyone can provide any
explanation/suggestions/workarounds to my problem? Thanks a lot
in advance!
Reference:
Articles 1.
ASP.NET 2.0 Localization Features: A Fresh Approach to Localizing Web Applications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/ASP2local.asp
Articles 2.
ASP.NET 2.0 Internals
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/internals.asp
Articles 3.
The ASP.NET Page Object Model
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-pageobjectmodel.asp
Zhu Ming