Best way to implement language menu in ASP.NET application

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

The menu will appear on several main pages (currently one for pages where users are logged in and one for pages with no users).

My current implementation has a single master page base class (let me call it MasterBase). MasterBase has an event

public event LanguageChangedEventHandler LanguageChanged;

      

where LanguageChangedEventHandler is simply defined as

public delegate void LanguageChangedEventHandler(string NewCulture);

      

MasterBase also has the ability to overcome

protected virtual void OnLanguageChanged(string NewCulture)

      

which just fires the event.

Each master page that inherits MasterBase overrides OnLanguageChanged and does the usual things like set Current CurrentUICulture and language cookie, then does

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

      

to load a page with localized values ​​for the new culture. On the main page for registered users, it also updates the pref language in db.

Each language setting is currently a LinkButton on the master page that inherits from MasterBase. When the link is clicked, it calls the base OnLanguagedChanged method passing the correct culture information. For example.

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

      

Each page that needs to handle the 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 confusing 'framework' =)

  • First, it is difficult for new developers to understand that they have to hook a method to the MasterBase LanguageChanged event to handle language changes. Yes, we document it. Yet this is not something straightforward and obvious.
  • Second, all language changes are feedback. This is especially problematic if you want to go back using the Back to Back button.

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

I really appreciate any suggestions. Thank.

+1


source to share


2 answers


It seems to me that it would be better to implement this in a control that sets an application variable that all pages can use. This way you can just implement the code in one place and always have it available on every page that renders the control (maybe in your master so that all pages that inherit automatically get it). I think that in the control you will have a handler that sets the global language and then reloads the page. Each page checks the language settings during page_load or prerender and loads the corresponding localized strings accordingly.



+1


source


I would just use an event PreInit

on the base page to set the current ui culture. I don't understand why you need each page to know when the language is changed.



+1


source







All Articles