locked
Best place to override Culture RRS feed

  • Question

  • User-574293449 posted

    hi,

      I have an web applciation which using multilingual. Here in all pages,  Intialize culture is override. I am thinking a single place to set this culture so that no need to override again in all pages. I read and got some of the places

    Application_BeginRequest in global.asax

    Session_BeginRequest in global.asax

    OnAcquireRequestState in glabal,asax

    My concern is after user logged i set the user prefereed culture in session. After that only i can set the UI culture for the user to display the prefereed language. So where i can implement the code to set the culture from the session value. But not override again in my master page or other pages

    Friday, January 17, 2014 3:21 AM

Answers

  • User-1509636757 posted

    I am thinking a single place to set this culture so that no need to override again in all pages

    You are correct. That is the general practice. I would suggest to create a BasePage and inherit this page in all your aspx page. And write localizatoin overring code in that BasePage:

    //-- inherit from BasePage class
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
    
    //-- create this class in App_Code folder in a separate file say, called  "BasePage.cs"
    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            if (Session["appCultureSession"] != null)
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Convert.ToString(Session["appCultureSession"]));
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Convert.ToString(Session["appCultureSession"]));
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            }
        }
    }

    This way, you will have to create all pages by inheriting from "BasePage" and dont have to write overriding code in all pages.

    hope it helps./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 17, 2014 3:58 AM

All replies

  • User-1509636757 posted

    I am thinking a single place to set this culture so that no need to override again in all pages

    You are correct. That is the general practice. I would suggest to create a BasePage and inherit this page in all your aspx page. And write localizatoin overring code in that BasePage:

    //-- inherit from BasePage class
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
    
    //-- create this class in App_Code folder in a separate file say, called  "BasePage.cs"
    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            if (Session["appCultureSession"] != null)
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Convert.ToString(Session["appCultureSession"]));
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Convert.ToString(Session["appCultureSession"]));
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            }
        }
    }

    This way, you will have to create all pages by inheriting from "BasePage" and dont have to write overriding code in all pages.

    hope it helps./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 17, 2014 3:58 AM
  • User-574293449 posted

    Thanks great work. but If i use Application_BeginRequest event in Http Handler , is this better or any disadvantage other than session inaccessable

    I also have Master pages. So no option to display other language in master pages

    Friday, January 17, 2014 4:12 AM
  • User-1509636757 posted

    Application_BeginRequest event

    Only problem is you will not be able to set the session; which in this case for overriding localization and use the same localization on further postbacks.. will be difficult without setting session.

    I also have Master pages. So no option to display other language in master pages

    sorry, but I did not get you on this part, if you can be more clear.

    Friday, January 17, 2014 4:22 AM
  • User-574293449 posted

    In the first answer yes i just set the cookie and so that in the application_beginrequest it is updated.  I din't get what you told about session problem. For every request this method will call. am i right?

     

    For the second one, i mean, i also have master pages. So if i inherit the base file to all pages what happened to Master pages. If i add one label to master page, is the current ui updated in master page?

    Friday, January 17, 2014 4:31 AM
  • User-1509636757 posted

    I din't get what you told about session problem. For every request this method will call.

    Yes, but you may find Session always null in application_beginrequest event.

    For the second one, i mean, i also have master pages. So if i inherit the base file to all pages what happened to Master pages. If i add one label to master page, is the current ui updated in master page?

    You dont have to do anything for masterpage, As the content page (inside masterpage) which is having the code for overriding InitializeCulture, will handle localization for Masterpage as well.

    hope it helps./.

    Friday, January 17, 2014 4:41 AM
  • User-574293449 posted

    oh k great.  I can go with this.

    Can you give why not use Application_BeginRequest. Sorry i not got exactly the disadvatage in my case. Actually in my login page (not from master page) i set the cookie and this will not chnage after relogin by any other user. So in my Application_BeginRequest i can use the cookie and set the currentUICulture.  Aain this is for my understanding please

     

    Friday, January 17, 2014 4:48 AM
  • User-1509636757 posted

    Can you give why not use Application_BeginRequest

    Oh! if you are not using Session to store the cultureinfo (and using cookie) then it is fine to use Application_BeginRequest. I was denying because of session; as you will find session null in Application_BeginRequest event. For cookie; I think that is fine to use cookie to store cultureinfo within Application_BeginRequest.

    Friday, January 17, 2014 5:01 AM
  • User-574293449 posted

    k thanks friend

    Friday, January 17, 2014 5:04 AM
  • User-1939249872 posted

    But How to set it in Master Page?

    akhilrajau

    I am thinking a single place to set this culture so that no need to override again in all pages

    You are correct. That is the general practice. I would suggest to create a BasePage and inherit this page in all your aspx page. And write localizatoin overring code in that BasePage:

    //-- inherit from BasePage class
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }
    
    //-- create this class in App_Code folder in a separate file say, called  "BasePage.cs"
    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            if (Session["appCultureSession"] != null)
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Convert.ToString(Session["appCultureSession"]));
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Convert.ToString(Session["appCultureSession"]));
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            }
        }
    }

    This way, you will have to create all pages by inheriting from "BasePage" and dont have to write overriding code in all pages.

    hope it helps./.

    Tuesday, January 28, 2014 5:43 PM
  • User-574293449 posted

    I didn't get you. You asking in master page. That only explained that there is no culture override in master page. Instead you need to override in base page so that all inherited page will call this base page method. Maste rpage is also set in your pages so that autoamtically the culture is override in the master also

    Tuesday, January 28, 2014 11:03 PM