Login

Specifying what language to load

Versions: 4.30 - 4.51, FAQ number: 137, Old FAQ number: 3841
Note: This faq does not apply to the globalization support in version 4.60 and later!

 

Setting what language to load

There are several ways to determining what language a page should be loaded with. For regular site visitors this can be done with cookies, the session object query string or personal settings for authenticated users. If you want to update a page manually or set the langauge for the page depending on the url of the request HttpContext.Items is good way. The following example shows how to set the language to the value of a dynamic property if the user doesn't already have a langauge specified in the session object. This code is placed in the default framework:

private void Page_Init(object sender, System.EventArgs e)
  {
   SetLanguage();
  }
  private void SetLanguage()
  {   
   if(HttpContext.Current.Request.QueryString["epslanguage"] != null)
    return;
   if(HttpContext.Current.Session != null && ((string)HttpContext.Current.Session["epslanguage"]) != string.Empty)
    return;

   EPiServer.Core.PageData currentPage = null;
   currentPage = GetPage(((PageBase)Page).CurrentPageLink);
   string language = currentPage["defaultlanguage2"] == null ? string.Empty :  (string)currentPage["defaultlanguage2"];

   HttpContext.Current.Items["epslanguage"] = language;
  }

 

You might have noticed that the method does not set a langauge if there is a value specified in the query string. You have the option to override the value from the query string but you should not do this as this is used to specify what langauge you want when previewing a page.

If you want to set the language depending on the site url a good way is to add your logic to the Application_BeginRequest event in global.asax.

Setting the language when HttpContext is not available

When you are in an environment without HttpContext you can instead use the following to specify the language:

System.Runtime.Remoting.Messaging.CallContext.SetData("epslanguage", YourLanguage)

Note: This only works in EPiServer 4.50 and later versions.

EPiTrace logger