views:

463

answers:

2

I trying to implement a typical languages menu where users can select the language they want to view the site in through a menu that appears throughout all pages in the site.

The menu will appear on multiple master pages (currently one for pages where users are logged in and one for pages where users are not).

My current implementation is having a single master page base class (let's call it MasterBase). MasterBase has an event

public event LanguageChangedEventHandler LanguageChanged;

where LanguagedChangedEventHandler is simply defined as

public delegate void LanguageChangedEventHandler(string NewCulture);

MasterBase also has an overridable method

protected virtual void OnLanguageChanged(string NewCulture)

which just basically fires the event.

Each master page that inherits MasterBase overrides OnLanguageChanged and does the usual stuff like set the Thread's CurrentUICulture and the language cookie then does a

Server.Transfer(this.Page.AppRelativeVirtualPath, true);

to get the page to reload with localized values for the new culture. On the master page for logged in users it also updates the user's language pref in the db.

Each language option is currently a LinkButton on a master page that inherits from MasterBase. When the link is clicked it calls the base OnLanguagedChanged method passing in the correct culture info. E.g.

protected void btnEnglish_Click(object sender, EventArgs e) {
    this.OnLanguageChanged("en-US");
    }

Each page that needs to handle a language change then has some code in the page load that looks like...

((MasterBase)this.Master).LanguageChanged += this.Page_OnLanguageChanged;
// Where Page_OnLanguageChanged has the signature of LanguageChangedEventHandler
// and has stuff like reloading options in a drop down using the new language.

Quite a convoluted 'framework' =)

  1. Firstly it's hard for new developers to know they have to hook up a method to the MasterBase's LanguageChanged event to handle language changes. Yes, we do document it. But still it's not something straightforward and obvious.
  2. Secondly, all language changes are post backs. This is problematic especially when you want to navigate back with the browser Back button.

I'm looking for a more elegant solution. One that doesn't have both the problems above and also handles my current requirements.

Greatly appreciate any suggestions. Thanks.

+1  A: 

It seems to me that it would be better to implement this in a control that sets an application variable that all pages could use. That way you could just implement the code in one place and have it always available on each page that displays the control (could be in your master's so all pages that inherit get it automatically). I think in the control you would have a handler that sets the global language setting and then reloads the page. Each page would check the language setting during page_load or prerender and load the proper localized strings accordingly.

tvanfosson
Helpful but still doesn't solve the problem of not requiring a postback.
fung
+1  A: 

I would just use the PreInit event on base page to set the current ui culture. I am not clear on why you would need each page to know when language is changed.

muratgu