public ActionResult ChangeCulture(string id) { string returnUrl = Request.UrlReferrer.PathAndQuery; List <string> cultures = Lang.GetISOCodes(); if (!cultures.Contains(id)) { id = Lang.DefaultLang(); } HttpCookie cookie = Request.Cookies["lang"]; if (cookie != null) { cookie.Value = id; } else { cookie = new HttpCookie("lang"); cookie.HttpOnly = false; cookie.Value = id; cookie.Expires = DateTime.Now.AddYears(1); } Response.Cookies.Add(cookie); return(Redirect(returnUrl)); }
public void OnActionExecuted(ActionExecutedContext filterContext) { string cultureName = null; // Получаем куки из контекста, которые могут содержать установленную культуру HttpCookie cultureCookie = filterContext.HttpContext.Request.Cookies["lang"]; if (cultureCookie != null) { cultureName = cultureCookie.Value; } else { var userLanguages = HttpContext.Current.Request.UserLanguages; if (userLanguages[0] != null) { cultureName = userLanguages[0]; } else { cultureName = Lang.DefaultLang(); } } // Список культур List <string> cultures = Lang.GetISOCodes(); if (!cultures.Contains(cultureName)) { cultureName = Lang.DefaultLang(); } CultureInfo ci = CultureInfo.GetCultureInfo(cultureName); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; }