Sitecore : Changing language removes your parameters

Sitecore 4 have the simplest way of choosing language. Just add a link with the parameter ?lg=language (where language is the language to switch to, DA for example).

Many of my websites is multilanguage sites. At the top of each page I have thses “Change language” links listed:

<a href="?lg=da">Danish</a>
<a href="?lg=en">English</a>

But what happens if the page itself is called with parameters? The messageboard for example gets the post id as parameter. In this case my language links will make the page fail.

I developed this small function that returns all current parameters formatted as a new querystring so the parameters can be appended to my links:

using System.Web;
using System.Text;
public static string Parameters(bool firstParameterAvailable)
  string Exclude = "[path][layout][lang]";
  StringBuilder sb = new StringBuilder();
  NameValueCollection qs = HttpContext.Current.Request.QueryString;
  foreach (string s in qs.AllKeys)
    if (Exclude.IndexOf( "["+s+"]" ) == -1)
      if (sb.Length == 0 && !firstParameterAvailable)
        sb.AppendFormat("?{0}={1}", s, qs[s]);
        sb.AppendFormat("={1}", s, qs[s]);
  return sb.ToString();

I then include the function in an XslExtension class and can then use it from my XSLT code:

<a href="{Parameters(1)}">UK</a>

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 4 and tagged , . Bookmark the permalink.

One Response to Sitecore : Changing language removes your parameters

  1. Well this article helped me. Thank you :)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s