Episerver 7 – Creating A Language Cookie And Ensuring The Page Uses It

In today’s guide I will cover how to set-up a website to run in a multi-lingual environment.

Step One – Creating A Default Language Cookie

In MVC we can use a global ActionFilter to ensure certain functionality is triggered when a user navigates to one of our webpages.  Writing a global action filter will allow us to add in some extra logic  and apply it across multiple controllers and/or actions.

The first thing our Action Filter will need to do is to check if our language cookie already exists.  If it doesn’t then set a default language of the page the visitor is browsing.

public class LanguageCookieACtionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var langCookie = filterContext.HttpContext.Request.Cookies["CountryCode"];
if (langCookie == null)
{
var currentCulture = ContentLanguage.PreferredCulture;
var languageBranch = currentCulture.Name;
langCookie = new HttpCookie("CountryCode", languageBranch);
filterContext.HttpContext.Request.Cookies.Add(langCookie);
}
base.OnActionExecuting(filterContext);
}
}

Step Two : Getting A Reference in ActionFilterAttribute to the Current Page

The next part of the dilemma is then switching the language of the current request, if it doesn’t match the language specified in the cookie. The first thing we need to do is get access to the page object.  As we’re in an Action Filter, we do not have direct access to the API, instead we can get it using this snippet:

var currentPage = filterContext.ActionParameters["currentPage"] as PageData;

Step Three : Switching The Language

The next snippet we will need is a way to switch the language. This can be done using this snippet.

EPiServer.BaseLibrary.Context.Current["EPiServer:ContentLanguage"] = new CultureInfo(languageValue);

Last Step : Putting It All Together

public class LanguageCookieACtionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var langCookie = filterContext.HttpContext.Request.Cookies["CountryCode"];
if (langCookie == null)
{
var currentCulture = ContentLanguage.PreferredCulture;
var languageBranch = currentCulture.Name;
CookieHelper.SetCookie("CountryCode", languageBranch, null);
}
else
{
var languageValue = CookieHelper.GetCookie("CountryCode");
if (ContentLanguage.PreferredCulture.Name != languageValue)
{
EPiServer.BaseLibrary.Context.Current["EPiServer:ContentLanguage"]
= new CultureInfo(languageValue);
var currentPage = filterContext.ActionParameters["currentPage"] as PageData;
var control = filterContext.Controller as Controller;
if (control != null && currentPage != null)
{
control.HttpContext.Response.Redirect(currentPage.LinkURL);
}
}
}
base.OnActionExecuting(filterContext);
}

All the cookie get/set is done using this helper method

public static class CookieHelper
{
public static void SetCookie(string key, string value, TimeSpan? expires)
{
var encodedCookie = new HttpCookie(key, value);
if (HttpContext.Current.Request.Cookies[key] != null)
{
var cookieOld = HttpContext.Current.Request.Cookies[key];
cookieOld.Expires = SetExpireTime(expires);
cookieOld.Value = encodedCookie.Value;
HttpContext.Current.Response.Cookies.Add(cookieOld);
}
else
{
encodedCookie.Expires = SetExpireTime(expires); ;
HttpContext.Current.Response.Cookies.Add(encodedCookie);
}
}
private static DateTime SetExpireTime(TimeSpan? expires)
{
return expires.HasValue ? DateTime.Now.Add(expires.Value) : DateTime.Now.AddDays(1);
}
public static string GetCookie(string key)
{
var value = string.Empty;
var cookie = HttpContext.Current.Request.Cookies[key];
if (cookie != null)
{
value = cookie.Value;
}
return value;
}
}

This covers everything you need to have in a site that works via a language cookie. In the next tutorial Creating A Multi Language Picker In Episerver I’ll explain how to create a language picker. This picker will provide the functionality to let site visitors decide which language the site will render itself.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *