How to switch language on the runtime in asp.net2

W

walter

The question sound simple -- I had a multilingual site, I give a button on
each page to allow user switch to different language, and of cause I want to
use asp.net 2 globalization mechanism. So what I did is 1. make my controls
to use new resource expression, 2.create a session variable to record the
current language 3. and put a logic in page_load {
....Thread...Current...UICulture..= new
CultureInfo(..Session("LangCode")...); } .But when I traced the process, I
found the resource actually is populated before the page_load function.
Well, the reason I know this is because I created my own customized resource
provider factory inheriting from ResourceProviderFactory, and I found the
process sequence is Provider.GetObject()., then Page_Load().

Well, I can try to put page_init event , but I guess someone here must know
the solution already. Appreciate any comment.
 
W

walter

Juan, thanks for the information. I think there are two problems in your
suggestion :

1. Cache is on application level whereas my requirement is on per
user/session level.Well, even I can play with it ,but it looks like a bit
overkill.

2. I just did a trace, actually the resource is retrieved even before the
"Begin PreInit" event, which means in order to switch the language , I have
to put information somewhere, then resubmit the page again. It's not a nice
thing, do you agree?

I'm kind of stuck here. Welcome any comment.
 
J

Juan T. Llibre

re:
2. I just did a trace, actually the resource is retrieved even before the "Begin PreInit" event,
which means in order to switch the language , I have to put information somewhere, then resubmit
the page again.

The InitializeCulture method is called very early in the page life cycle,
before controls are created or properties are set for the page.

Therefore, to read values that are passed to the page from controls,
you must get them directly from the request using the Form collection.

There's a good explanation for the process, and sample code, at :

http://msdn2.microsoft.com/en-us/library/bz9tc508(VS.80).aspx

For a backgrounder on methods of implementing ASP.NET web page resources
( although you look quite adept at it already ), see :

http://msdn2.microsoft.com/en-us/library/ms227427.aspx
 
W

walter

Juan, thanks a lot.That doc solve my problem . An additional question is on
the my CustomeResourceProvider --I want to store the resource in database.
But looks like I need to implement a language tree logic in my SQL tier to
find the proper resource root if the current culture is not available. Just
want to confirm if this is the right understanding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top