EPiServer Edit Mode Language and Culture Selection Bug Fix

EPiServer 7 have annoying bug that it uses wrong culture for showing resource texts and datetimes etc. You can read more about the bug from here: http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=71724. This problem can be fixed with couple of small modifications. I have use this fix in couple of projects and solution has worked every time without any problems. Fix is basically simple. We just set the culture manually in our project base controller (I expect every project has one).

namespace Huilaaja.Net.HotFixes
{
    /// summary
    /// All controllers that renders pages should inherit from this class.
    /// /summary
    public abstract class PageControllerBase : PageController
        where T : PageData
    {

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            FixEditModeCurrentCulture.TryToFix(filterContext);
            base.OnActionExecuting(filterContext);
        }

    }
}
  The other part is trying to get the preferred culture efficiently.

using System;
using System.Globalization;
using System.Web.Mvc;
using EPiServer.Core;
using EPiServer.Web.Routing;

namespace Huilaaja.Net.HotFixes
{
    public static class FixEditModeCurrentCulture
    {
        /// 
        /// This method tries to fix a bug in episerver edit mode culture and resource text.
        /// http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=71724 
        /// When this bug is fixed. This class is useless.
        /// 
        public static void TryToFix(ActionExecutingContext filterContext)
        {
            if (EPiServer.Editor.PageEditing.PageIsInEditMode)
            {
                var prefCulture = GetEditModePreferredCulture(filterContext);
                if (prefCulture != null)
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = prefCulture;
                    System.Threading.Thread.CurrentThread.CurrentCulture = prefCulture;
                }
            }
        }


        /// 
        /// This document describe how "Determing Content Language" should work and this method imitates it.
        /// http://sdkbeta.episerver.com/SDK-html-Container/?path=/SdkDocuments/CMS/7/Knowledge%20Base/Developer%20Guide/Globalization/Language%20Settings.htm&vppRoot=/SdkDocuments//CMS/7/Knowledge%20Base/Developer%20Guide/
        /// 
        public static CultureInfo GetEditModePreferredCulture(ActionExecutingContext filterContext)
        {
            string epslanguage = filterContext.HttpContext.Request.Params["epslanguage"];
            if (!string.IsNullOrEmpty(epslanguage))
                return CultureInfo.GetCultureInfo(epslanguage);

            System.Web.HttpCookie editlanguagebranch = filterContext.HttpContext.Request.Cookies["editlanguagebranch"];
            if (editlanguagebranch != null && !string.IsNullOrEmpty(editlanguagebranch.Value))
                return CultureInfo.GetCultureInfo(editlanguagebranch.Value);

            ContentReference link = filterContext.HttpContext.Request.RequestContext.GetContentLink();
            if (!ContentReference.IsNullOrEmpty(link))
            {
                var loader = ServiceLocator.Current.GetInstance();
                var p = loader.Get(link, LanguageSelector.AutoDetect(true)) as PageData;
                if (p != null)
                    return p.Language;
            }
            return null;
        }
    }

}
Other option would be to get the current culture straight from the current page, but because the current page is not really known yet, I thought this solution wouldn't affect performance as much.
EPiServer 7 have annoying bug that it uses wrong culture for showing resource texts and datetimes et…