Exemple #1
0
        public void SetThreadLanguageAutomatic()
        {
            var context = HttpManager.CurrentContext;

            // No cookies, no accept languages, with default language
            context.RemoveCookie(LocaleUtils.LanguageKey);
            Assert.IsTrue(LocaleUtils.SetThreadLanguageAutomatic(false, "zh-CN"));
            Assert.Equals(CultureInfo.CurrentCulture.Name, "zh-CN");
            Assert.Equals(CultureInfo.CurrentUICulture.Name, "zh-CN");
            Assert.IsTrue(LocaleUtils.SetThreadLanguageAutomatic(false, "en-US"));
            Assert.Equals(CultureInfo.CurrentCulture.Name, "en-US");
            Assert.Equals(CultureInfo.CurrentUICulture.Name, "en-US");
            // No cookies, have accept languages but not using, no default language
            using (HttpManager.OverrideContext("", "GET")) {
                var request = (HttpRequestMock)HttpManager.CurrentContext.Request;
                request.headers["Accept-Language"] = "NotExist,zh-CN;q=0.7";
                Assert.IsTrue(!LocaleUtils.SetThreadLanguageAutomatic(false, null));
                Assert.Equals(CultureInfo.CurrentCulture.Name, "en-US");
                Assert.Equals(CultureInfo.CurrentUICulture.Name, "en-US");
            }
            // No cookies, have accept languages and it can be used, no default language
            using (HttpManager.OverrideContext("", "GET")) {
                var request = (HttpRequestMock)HttpManager.CurrentContext.Request;
                request.headers["Accept-Language"] = "NotExist,zh-CN;q=0.7";
                Assert.IsTrue(LocaleUtils.SetThreadLanguageAutomatic(true, null));
                Assert.Equals(CultureInfo.CurrentCulture.Name, "zh-CN");
                Assert.Equals(CultureInfo.CurrentUICulture.Name, "zh-CN");
            }
            // Have cookies, no accept languages, no default language
            context.PutCookie(LocaleUtils.LanguageKey, "en-US");
            Assert.IsTrue(LocaleUtils.SetThreadLanguageAutomatic(false, null));
            Assert.Equals(CultureInfo.CurrentCulture.Name, "en-US");
            Assert.Equals(CultureInfo.CurrentUICulture.Name, "en-US");
        }
        /// <summary>
        /// 处理请求
        /// </summary>
        public void OnRequest()
        {
            var configManager  = Application.Ioc.Resolve <GenericConfigManager>();
            var localeSettings = configManager.GetData <LocaleSettings>();

            LocaleUtils.SetThreadLanguageAutomatic(
                localeSettings.AllowDetectLanguageFromBrowser, localeSettings.DefaultLanguage);
            LocaleUtils.SetThreadTimezoneAutomatic(localeSettings.DefaultTimezone);
        }
 /// <summary>
 /// 处理请求
 /// </summary>
 public void OnRequest()
 {
     LocaleUtils.SetThreadLanguageAutomatic(true, "en-US");
 }