Changing the Sitecore Language Format in the URL

I was recently thinking about an issue I had when I first started working with Sitecore. I live in Canada so the majority of websites are built in English and French which is handled nicely by Sitecore. The problem was that I didn’t know what to do when a client asked for the language to appear in the URL as “fr” instead of “fr-CA”.

This is a valid request since the URLs will be rendered as shown below after adding the French Canadian language to Sitecore and using the “languageEmbedding” setting in the Sitecore link provider:

Instead of something a little more intuitive such as:

I have to admit that I was stumped and had a lot of problems with the language versions as a Sitecore rookie. I’ll take you through my learning process and show you how to change the format of the language that appears in the URL.

In the following example I have added the French (Canada) language and the French (France) language but I will be focussing on French (Canada). I started off by adding the languages to the content tree under the “/sitecore/system/Languages” node.

sitecore_languages1

For this example I went and added a French (Canada) version to standard Sitecore home page and navigated to the page using the “fr-CA” slug in the URL.

sitecore_languages13

So now I have a French version but I’m looking for the URL to display “fr” instead of “fr-CA”. The good news is that it’s actually quite easy to accomplish this by changing the item name for a language item in Sitecore. For example, I renamed the “fr-CA” item to “fr”.

languages_fr

This actually works perfectly if you have not added any versions for the old language to Sitecore items. In my example above, if I had added the “fr-CA” language and immediately renamed it then everything would be perfect and I would be left with only the “fr” language and no trace of the “fr-CA” language.

Unfortunately if you have already added some item versions in the original language (like I did for the fr-CA home page example above) then there are a couple extra steps to clean things up. Since I added a “fr-CA” version to my home page, if I were to navigate back and view its languages I would see that it contains both the “fr” version and the “fr-CA” version. This is definitely not what I expected.

sitecore_languages3

I’m looking to remove the French (Canada) version. Although it’s counter-intuitive, the easiest way I know of for removing the old language versions is to actually add the old language back into the list of languages under the /sitecore/system/Languages” node and then immediately delete it again. Yes, it’s a bit strange but deleting the language will remove this language version from every item in Sitecore.

languages_fr-CA

So now you can finally start adding the “fr” language versions to your site and they will have the proper format in the URL.

sitecore_languages11

Disclaimer: The strategy outlined above works great at the beginning of a project when you are not working with real content. Following the steps outlined above will actually remove the previous language version all together and you will lose any content associated with that language. In my next post I will outline a strategy for the case where you want to preserve existing content.

 

Advertisements
Posted in Sitecore

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s