EPiServer 7 : An Alternative EPiServer Multi-Lingual Approach

I think most developers know how the multi-language feature of EPiServer works,  In the admin you define the languages you want in the site, for each property you add the ‘[CultureSpecific(true)]’ attribute to the properties to specify the property that it should have a different value for each language, you create a language switcher and that’s it.  In the editor you will have one section in navigation tree and the content editor will switch the language to add in different content.  

Unfortunately, this model for some organisations doesn’t work.  I’ve worked on several projects for very large organisations who have  multiple businesses across the world and for each business the site’s content (pages images, content) needs to be flexible, e.g. you should be able to change the layout for one without affecting everything else.  In these examples what you really want is a section per language/business so you add pages to one branch without it affecting every other section.

There are several ways you can implement this functionality but if you find yourself with a similar requirement, after a lot of trial and error,  I recommend the following approach:.  The solution will involve three main files:

  • Start Page template:  This will have no content at all.  All the template will do is redirect the user to the appropriate country landing page.  This will be based on if they have an existing country code cookie set, or, based on IP.
  • Home Page template, This template will be the homepage for each individual country
  • An ActionFilterAttribute to set a default cookie if one doesn’t exist.

 

 LanguageCookieActionFilter

 

public class LanguageCookieActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var langCookie = filterContext.HttpContext.Request.Cookies[GlobalResourceDefinitions.CookieNames.CountryCode];
if (langCookie == null)
{
CookieHelper.SetCookie(GlobalResourceDefinitions.CookieNames.CountryCode, GlobalResourceDefinitions.CookieNames.DefaultCountryCode, null);
}
base.OnActionExecuting(filterContext);
}
}

Start Page Controller

 

public class StartPageController : BasePageController<StartPage>
{
private HttpCookie _countryIdCookie;
private HttpCookie _countryGatewayCookie;
public ActionResult Index(StartPage currentPage)
{
_countryIdCookie = Request.Cookies.Get(GlobalResourceDefinitions.CookieNames.CountryHomepageIdCookie);
_countryGatewayCookie = Request.Cookies.Get(GlobalResourceDefinitions.CookieNames.CountryCode);
var url = string.Empty;
if (_countryGatewayCookie != null)
{
url = GetHomePageCountryGateway(_countryGatewayCookie.Value);
}
if (_countryIdCookie != null && string.IsNullOrEmpty(url))
{
var countryCookieValue = _countryIdCookie.Value;
int homePageId;
int.TryParse(countryCookieValue, out homePageId);
if (homePageId == 0)
{
RedirectToError(currentPage);
};
}
return !string.IsNullOrEmpty(url) ? Redirect(url) : RedirectToError(currentPage);
}
private ActionResult RedirectToError(StartPage currentPage)
{
return View("Index", new StartPageViewModel(currentPage));
} 
private string GetHomePageCountryGateway(string countrygatewayCode)
{
var countryPage = DataFactory.Instance.GetChildren(ContentReference.StartPage)
.Where(child => child is HomePage).Cast<HomePage>().FirstOrDefault(x => x.ShortCountryCode == countrygatewayCode);
return countryPage != null ? countryPage.LinkURL : string.Empty;
}
}

Homepage Controller

 

public class HomePageController : BasePageController<HomePage>
{
public ActionResult Index(HomePage currentPage)
{
Response.Cache.SetExpires(DateTime.MinValue);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetMaxAge(TimeSpan.Zero);
Response.Cookies.Remove(GlobalResourceDefinitions.CookieNames.CountryCode);
var cookieFactory = new CookieFactory();
//remove unwanted old homepage cookies if they exist
if (Request.Cookies[CookieFactory.OldCountryHomepageCookie] != null)
{
Response.AppendCookie(cookieFactory.CreateOldCountryHomepageCookie("0"));
Response.AppendCookie(cookieFactory.CreateOldCountryHomepageCookieWithDomain("0"));
}
var codeCookie = new HttpCookie(GlobalResourceDefinitions.CookieNames.CountryCode)
{
Value = currentPage.ShortCountryCode,
Expires = DateTime.Now.AddYears(1),
Path = "/"
};
Response.AppendCookie(codeCookie);
return View("Index", new HomePageViewModel(currentPage));
}
}

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

2 replies

Trackbacks & Pingbacks

  1. […] automatically instead of making them manually changed it. If you haven’t read my article An Alternative EPiServer Multi-Lingual Appraoch then that details a good use for using the GeoIp Locator. Another good example would be setting a […]

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 *