示例#1
0
        // Sets the thread's CurrentCulture and CurrentUICulture to those associated
        // with the current HttpContext. We do this since the culture of a request can
        // change over its lifetime and isn't necessarily the default for the AppDomain,
        // e.g. if the culture was read from the request headers.
        private void SetRequestLevelCulture()
        {
            CultureInfo culture   = null;
            CultureInfo uiculture = null;

            GlobalizationSection globConfig = RuntimeConfig.GetConfig(HttpContext).Globalization;

            if (!String.IsNullOrEmpty(globConfig.Culture))
            {
                culture = HttpContext.CultureFromConfig(globConfig.Culture, true);
            }

            if (!String.IsNullOrEmpty(globConfig.UICulture))
            {
                uiculture = HttpContext.CultureFromConfig(globConfig.UICulture, false);
            }

            if (HttpContext.DynamicCulture != null)
            {
                culture = HttpContext.DynamicCulture;
            }

            if (HttpContext.DynamicUICulture != null)
            {
                uiculture = HttpContext.DynamicUICulture;
            }

            // Page also could have its own culture settings
            Page page = HttpContext.CurrentHandler as Page;

            if (page != null)
            {
                if (page.DynamicCulture != null)
                {
                    culture = page.DynamicCulture;
                }

                if (page.DynamicUICulture != null)
                {
                    uiculture = page.DynamicUICulture;
                }
            }

            _originalThreadCurrentCulture   = Thread.CurrentThread.CurrentCulture;
            _originalThreadCurrentUICulture = Thread.CurrentThread.CurrentUICulture;

            if (culture != null && culture != Thread.CurrentThread.CurrentCulture)
            {
                HttpRuntime.SetCurrentThreadCultureWithAssert(culture);
            }

            if (uiculture != null && uiculture != Thread.CurrentThread.CurrentUICulture)
            {
                Thread.CurrentThread.CurrentUICulture = uiculture;
            }
        }
示例#2
0
        // Restores the thread's CurrentCulture and CurrentUICulture back to what
        // they were before this ThreadContext was associated with the thread. If
        // any culture has changed from its original value, we squirrel the new
        // culture away in HttpContext so that we can restore it the next time any
        // ThreadContext associated with this HttpContext is active.
        private void RestoreRequestLevelCulture()
        {
            CultureInfo currentCulture   = Thread.CurrentThread.CurrentCulture;
            CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;

            if (_originalThreadCurrentCulture != null)
            {
                // Avoid the cost of the Demand when setting the culture by comparing the cultures first
                if (currentCulture != _originalThreadCurrentCulture)
                {
                    HttpRuntime.SetCurrentThreadCultureWithAssert(_originalThreadCurrentCulture);
                    if (HttpContext != null)
                    {
                        // remember changed culture for the rest of the request
                        HttpContext.DynamicCulture = currentCulture;
                    }
                }

                _originalThreadCurrentCulture = null;
            }

            if (_originalThreadCurrentUICulture != null)
            {
                // Avoid the cost of the Demand when setting the culture by comparing the cultures first
                if (currentUICulture != _originalThreadCurrentUICulture)
                {
                    Thread.CurrentThread.CurrentUICulture = _originalThreadCurrentUICulture;
                    if (HttpContext != null)
                    {
                        // remember changed culture for the rest of the request
                        HttpContext.DynamicUICulture = currentUICulture;
                    }
                }

                _originalThreadCurrentUICulture = null;
            }
        }