Force a page to display in a specific language dynamically

  • Feb 13, 2015
  • EPiServer
  • Globalization
  • |

EPiServer has a specific order in which the content language is set.

  1. Language indicator in the URL
  2. Language selection in edit view
  3. Host name associated with a specific URL, configured in the web.config
  4. Cookie ‘epslanguage’
  5. Setting pageUseBrowserLanguagePreferences in the web.config
  6. uiCulture setting in the web.config
  7. The first enabled language branche as defined in the EPiServer

Read more about this in the documentation.

For example if the cookie ‘epslanguage’ is set to the language ‘nl’ and the URL http://test.local/ is browsed then the Dutch version of the page will be displayed. If the visitor browse to an English page, for example http://test.local/en/search, the English version of the page is displayed. As you can see in the order list, at the beginning of the blog, the language indicator in the URL has a higher priority then the ‘epslanguage’ cookie. This behavior can be overridden, if you just want to display the Dutch version of the page even when a English page is called. This will create the following scenario.

URL Cookie ‘epslanguage’ Result language
http://test.local/

nl

nl

http://test.local/en

nl

nl

 

This can be done manually by configuring a replacement language in EPiServer for each page. Another option is to do this dynamically by creating an implementation of the IUpdateCurrentLanguage interface. This interface contains two methods, UpdateLanguage and UpdateReplacementLanguage. The UpdateLanguage is called by the routing system of EPiServer, it passes the language segment (for example http://test.local/en/contact) of the URL. This method will set the content language.

In the below example I’ve created an implementation of the IUpdateCurrentLanguage. The UpdateLanguage first check whether the passed languageId (the language segment in the URL) parameter and the ‘epslanguage’ cookie are not equal. If this is true then the content language is set to the language in the ‘epslanguage’ cookie else the languageId is set.

public class MyUpdateCurrentLanguage : IUpdateCurrentLanguage
    {
        public void UpdateLanguage(string languageId)
        {
            if (!string.IsNullOrEmpty(languageId) && !languageId.Equals(HttpContext.Current.Request.Cookies["epslanguage"].Value, StringComparison.InvariantCultureIgnoreCase))
            {
                ContentLanguage.Instance.SetCulture(HttpContext.Current.Request.Cookies["epslanguage"].Value);
            }
            else
            {
                ContentLanguage.Instance.SetCulture(languageId);
            }
             
            SystemLanguage.Instance.SetCulture();
            UserInterfaceLanguage.Instance.SetCulture();
        }
     
        public void UpdateReplacementLanguage(EPiServer.Core.IContent currentContent, EPiServer.Core.ILanguageSelectionSource languageSource)
        {
            var updatecurrentLanguage = new UpdateCurrentLanguage();
            updatecurrentLanguage.UpdateReplacementLanguage(currentContent, languageSource);
        }
    }

StructureMap can be used for using the custom implementation instead of the EPiServer implementation.

container.For<IUpdateCurrentLanguage>().EnrichAllWith(y => new MyUpdateCurrentLanguage());
Comments