Exemplo n.º 1
0
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            //If the session doesn't exist, culture probably doesn't apply to this request. For example, this will happen when favicon.ico is requested.
            if (HttpContext.Current.Session == null)
            {
                return;
            }

            //Figure out the culture of the current request.
            string cultureName;

            //See if we have a session variable. If not, check for a cookie from last time.
            if (!SessionWrapper.Exists("DisplayLanguage"))
            {
                //No session, let's check the cookie
                HttpCookie cook = HttpContext.Current.Request.Cookies["DisplayLanguage"];
                if (cook != null && cook.Value.Equals("fr-CA"))
                {
                    //Found French cookie
                    cultureName = "fr-CA";
                }
                else
                {
                    //No cookie or cookie not French, default to English-Canada
                    cultureName = "en-CA";
                }
                //Save in session for future requests
                SessionWrapper.Add("DisplayLanguage", cultureName);
            }
            else
            {
                //Session value exists, load it
                cultureName = SessionWrapper.Get("DisplayLanguage", "en-CA");
            }

            //Don't do anything if it's the same culture
            if (Thread.CurrentThread.CurrentUICulture.Name == cultureName)
            {
                return;
            }

            //Set the thread culture
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
            Thread.CurrentThread.CurrentCulture   = Thread.CurrentThread.CurrentUICulture;
        }