where to put the generic code to check for browser language?

We are building a website in three languages English, Spanish and German. On every web page that is being accessed by the user we need to check the language of the browser to retrieve the correct data.

Our question is what would be the best location for the generic code to check for language.

Should we put the code in the Page_Load of the master page since every page is build using the master page

Or should we have a CommonPage.cs which all other pages inherit from

Or do you have any other/better ideas What is the best practice

Thanks,

Newbie!



Answer this question

where to put the generic code to check for browser language?

  • JR-XNA

    I'd have the Session start event in the global fire a method to check it.

  • Chirag Patel

    I agree but he didn't say he had a framework (though he did ask for ideas so maybe he'll build one based on your suggestion). If he had a framework I'd probably suggest putting it in an http module since that's where we'd handle the events that the global would have dealt with were it not absent.

    Should he/you have a basepage that the masterpages inherit from That depends on the situation. I don't have a rule that says "use them always" but I do find myself using them in almost every project. One thing I've run into with this situation is that I've needed an intermediate class to allow for behaviors common to certain parts of the app and not other parts that shouldn't be included in the basepage. How would you approach this I'm curious.



  • Dnieto23

    It depends on the framework you use really. Global is one place if you dont have any. Usually, if you use a UI framework, you will have a base controller class and all ASPX will derive from it. Possibly your controller can be an ASPX page and the UI may be implemented as ASCX under that, with all such code within the ASPX.



  • Stas Kravets

    There are different ways to do it.

      i.Are you going to direct the user to land in the default browser language

      ii.Are  you are going to give the user the option to select the language that they want to use

    If you use the second one, i would say use cookie to set the language preference and retrieve the content accordingly reading the cookie value. This requires page level check.

    You can use the same page, set the language for the Current thread and use resource files to retrieve the language content. Or you can retrieve from the database , In your case how do you retrieve the language specific content

    Another way is having seperate page level content for each language,  and redirect to this virtual site based on the selection by the user selection/ browsers default language. but there  is a maintanance overhead.

    You  can expose a property corresponding to Request.UserLanguage array in the Master Page and call it in all the subsequent child pages. You can modify this property to use cookie also.



  • Toni Greco

    My way of Implementing functionalities common to a subset of a site It depends really - I use either of the following three based on the situation

    1) I prefer to make custom controls and distribute them in the pages required - if that will work.

    2) Otherwise, I use the same approach like you have highlighted - to use intermediate "helper" classes and call them.

    3) If by design, the common features are "hierarchical" (and not randomly required ) and If I dont use master pages, I can always have base class for the site, and child classes for the sections and have the different ASPX/ASCX extend the classes of that section. For some reason, I am not able to use this approach as many times as I would wish to.

    I am not a very big fan of master pages. Typically, the applications I work on require more than one kind of templates - possibly user sections look different than admin which look different from Printer friendly pages which look different from "white labelled" or co-branded sections of the site. Master pages dont let me have that - atleast to my knowledge.

    Pranshu



  • where to put the generic code to check for browser language?