public ActionResult MachineTranslation(string language) // shortcode
        {
            string   viewName    = "MachineTranslation";
            Language currentLang = InterfaceLanguages.GetLanguageByShortCode(language);

            ViewBag.langs = new string[] { "bg", "en", "de", "fr", "it", "pt", "pl", "nl" };
            if (!String.IsNullOrEmpty(language))
            {
                string shortLang;
                string fullViewName;

                shortLang    = language;
                fullViewName = viewName + shortLang;
                if (this.ViewExists(fullViewName))
                {
                    return(View(fullViewName, currentLang));
                }
            }

            currentLang = InterfaceLanguages.GetLanguageByShortCode("en");
            return(View(viewName + "en", currentLang));
        }
        public ActionResult ChangeLanguage(string shortCode)
        {
            var langModel = InterfaceLanguages.GetLanguageByShortCode(shortCode);

            if (langModel == null)
            {
                return(RedirectToAction("Index"));
            }

            //this.Language = langModel;
            Session["LanguageId"] = langModel.Id;
            var cookie = new HttpCookie("sitelang", langModel.Code)
            {
                HttpOnly = false,
                Secure   = false,
                Expires  = DateTime.Now.AddDays(365)
            };

            HttpContext.Response.Cookies.Add(cookie);

            return(RedirectToAction("Index"));
        }
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            System.Web.SessionState.HttpSessionState Session = System.Web.HttpContext.Current.Session;
            HttpRequest Request = System.Web.HttpContext.Current.Request;

            string[] host = Request.Url.Host.Split('.');
            if (Session != null && Session.IsNewSession && (host[0].ToLower() == "demo" || host[0].ToLower() == "freecases"))
            {
                // temporarily saving user agent strings, gotta catch that google bot.
                var userAgentString = Request.UserAgent;

                /*  using (StreamWriter sw = new StreamWriter(Server.MapPath("~/Logs/ua.txt"), true))
                 * {
                 *    sw.WriteLine(userAgentString);
                 * }*/
                // end of temp saving

                UserData ud = UserMng.Login("sysdemo", ConfigurationManager.AppSettings["DEMO_USER_PASS"], false, Request.UserHostAddress);
                if (ud != null)
                {
                    if (ud.SessionId > 0)
                    {
                        Session["UserData"] = ud;
                        Session.Timeout     = ud.SessionTimeout; // minutes

                        var productsList = UserMng.GetProductsList(ud.UserId);
                        Session["ProductsList"] = productsList;

                        var selectedProductCookie = Request.Cookies["SelectedProductId"];
                        int selectedProductId     = 1;

                        if (ud.Products.Where(p => p.IsActive.HasValue && p.IsActive == true).ToList().Count == 1)
                        {
                            selectedProductId = ud.Products.FirstOrDefault().ProductId;
                            var newCookie = new HttpCookie("SelectedProductId");
                            newCookie.Value = selectedProductId.ToString();

                            Response.SetCookie(newCookie);
                        }
                        else
                        {
                            if (selectedProductCookie != null && selectedProductCookie.Value != null && selectedProductCookie.Value.ToString() != "")
                            {
                                selectedProductId = int.Parse(selectedProductCookie.Value.ToString());
                            }
                        }
                        Session["SelectedProductId"] = selectedProductId;
                    }
                }
            }


            if (Session != null)
            {
                if (Request.Cookies["sitelang"] != null)
                {
                    Language currentLang = InterfaceLanguages.GetLanguageByCode(Request.Cookies["sitelang"].Value);
                    if (!currentLang.IsInterfaceLang) // some bad cookie
                    {
                        currentLang = null;
                    }
                    if (currentLang != null) // check if cookie contains valid language code, mmmmmm cookieeee (Cookie monster was here)
                    {
                        Session["LanguageId"] = currentLang.Id;
                    }
                }

                if (Session["LanguageId"] == null)
                {
                    Language preferedLang = null;
                    string   languageCode;

                    if (Request.UserLanguages != null)
                    {
                        foreach (var headerLang in Request.UserLanguages)
                        {
                            languageCode = headerLang.Split(';')[0];

                            if (languageCode.Length == 2)
                            {
                                preferedLang = InterfaceLanguages.GetLanguageByShortCode(languageCode);
                            }
                            else
                            {
                                preferedLang = InterfaceLanguages.GetLanguageByCode(languageCode);
                            }

                            if (preferedLang != null && !preferedLang.IsInterfaceLang)
                            {
                                preferedLang = null;
                            }

                            if (preferedLang != null)
                            {
                                break;
                            }
                        }
                    }

                    if (preferedLang == null)
                    {
                        preferedLang = InterfaceLanguages.GetLanguageById(Convert.ToInt32(ConfigurationManager.AppSettings["DefaultLanguageId"]));
                    }

                    Session["LanguageId"] = preferedLang.Id;

                    //HttpCookie cultureCookie = new HttpCookie("sitelang", defaultLang.Code);
                    //cultureCookie.Expires = DateTime.Now.AddYears(1);
                    //Response.Cookies.Add(cultureCookie);
                }

                string      code = InterfaceLanguages.GetLanguageById(Convert.ToInt32(Session["LanguageId"])).Code;
                CultureInfo ci   = new CultureInfo(code);
                System.Threading.Thread.CurrentThread.CurrentCulture   = ci;
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

                if (Request.Cookies["SelectedProductId"] != null)
                {
                    Session["SelectedProductId"] = Request.Cookies["SelectedProductId"].Value.ToString();
                }

                if (Session["SelectedProductId"] == null)
                {
                    Session["SelectedProductId"] = 1;
                }

                // Refreshing language to english in T&FSt if previously another not primary language has been selected. Sry for this. Blame it on the financists.
                if (Convert.ToInt32(Session["SelectedProductId"]) == 2 &&
                    Convert.ToInt32(Session["LanguageId"]) > 4 &&
                    Session["UserData"] != null &&
                    ((Session["UserData"] as UserData).ClientId != 1 || (Session["UserData"] as UserData).Username.ToUpper() == "SYSDEMO")) // Apis Europe client ignore
                {
                    Session["LanguageId"] = 4;                                                                                              // english
                    var newLangCookie = new HttpCookie("sitelang");
                    newLangCookie.Value = "en-GB";
                    newLangCookie.Expires.AddYears(365);
                    Response.SetCookie(newLangCookie);
                }
            }
        }