How to do a language switch in a master page. - preCharge Forums
It shows that you are unregistered. Please register with us by clicking Here
preCharge Forums


Nav Green LeftNav Right
preCharge Forums > Website Design & Development > Programming > .NET » How to do a language switch in a master page.


Reply
Tcat Right
 
LinkBack Thread Tools Display Modes Tcat Right
Old 07-06-2006   #1 (permalink)
gage
Junior Member
 
Join Date: Jul 2006
Age: 48
Posts: 6
Default How to do a language switch in a master page.

As the rap song goes: “There’s no champagne in the champagne room...”, there is no page in MasterPage. It derives from UserControl. As such it does not support the InitializeCulture() method for us to override as described in this post. Newbies love master pages, even though half the time they do not understand them, so here is a solution, compounding the misuse, but here it goes.
Declare a dropdown as usual in the master page. Instead of overriding the InitializeCulture() place the same code in the Application_BeginRequest event handler in the global.asax. It is very similar to InitializeCulture() in the sense that it occurs early and no controls are ready yet. We have a little problem though, now the control is declared in a template and its name and id attributes rendered differently. We can't use the control id and cheat like in the other post any more. We cant use the Control.UniqueID property either, remember no controls yet. So now what?
Another collection like the form variables collection that is also not originally server collection is the cookies collection. I use the cookie but it can be any of the ways for cross page communication that do not depend on server controls, like profile, session, querystring etc. We cannot use a cookie to carry the culture name value which comes from the dropdown selected item value, because the culture would always be a step behind. It would be set early on but later the dropdown selection change it but the resources for the previous culture come up. So normally only the page containing the dropdown would be a step behind, but because the dropdown is in the master page it appears that the whole site is ALWAYS ONE STEP BEHIND.
If, however, in the cookie, we pass the control name (which is information that never changes, so we can never be behind), instead of the culture name( which we can never keep up with), and then use that key to get the form variable value, we have pieced ourselves a workaround.
in the master page:
<aspropDownList runat="server" ID="DropDownList1" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged">
<asp:ListItem Value="en" Text="English" />
<asp:ListItem Value="fr" Text="French" />
<asp:ListItem Value="de" Text="German" />
</aspropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){
HttpCookie cookie = new HttpCookie("DropDownName");
cookie.Value=DropDownList1.UniqueID;
Response.SetCookie(cookie);
}

in global asax
void Application_BeginRequest(Object sender, EventArgs e){
string lang = string.Empty;//default to the invariant culture
HttpCookie cookie = Request.Cookies["DropDownName"];
if (cookie != null && cookie.Value != null)
lang = Request.Form[cookie.Value];
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}

Notice, there is absolutely no code in any of the content pages. Thats it ten lines of code and we are done for the whole site. This is not complete code, just a tip and trick of using master page and the .net event model to globalize code.
The new issue now is what about pages that do not have a master page? Not all pages are strapped in a template like that. This approach can be modified to support both. That is for next post.
Enjoy

Gage
gage is offline   Reply With Quote


Reply



Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch From One Forum Software To Another? Tucker Forum Management 3 12-28-2007 09:37 AM
1000s of Ebooks, Templates, Logos, Software,Scripts ALL WITH MASTER RESALE for $9 kibeth Content 1 12-02-2006 10:18 AM
Halo the movie guess whos master cheif? jamess Entertainment 5 09-25-2006 03:31 AM
Sachin Tendulkar: The master blaster yvsanil All Things General 2 08-02-2006 05:17 PM
Master Yoda Outlaw Graphics & Multimedia 12 09-25-2005 04:14 AM


footer left
All times are GMT. The time now is 08:31 AM.

DISCLAIMER: preCharge Risk Management is not responsible for any opinions, advice or comments expressed on the preCharge Community Forums.
preCharge® is a registered trademark of preCharge Risk Management | chargeback protection | Merchant Account Blog

Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC6

Internet Advertising | Car Credit | Mortgage | Loans | Music Festival

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49