retail-theatre_1424x397px

Avoid displaying pages without a language version

Posted 18 November 2011 6:31 PM by Marek Musielak

This post concerns Sitecore 6.5.0 (rev. 110818).

The Sitecore CMS allows you to create multiple versions of the pages for each of the languages supported by your site. Still, sometimes you may want to have particular pages translated into to a certain subset of the languages, e.g. if you have French content that should be available for end users in just France and Canada. The problem is that when you try to access the page in a language which does not have a supported version, you won't get a 404 error, but be displayed an empty page.

Now, to give you more context, assume that you want to run a competition targeted at just your UK customers. Our site has multiple languages as we have branches in many countries. We create a competition page for just the en language only. We publish the page, open it in a browser and we get:

sitecore - pages with no language version displayed anyway - language version exists

However, when we try to access the page in a language that has no translation for the page, surprisingly there is no 404 page, instead we get:

sitecore - pages with no language version displayed anyway - no language version

Despite the fact that there is no version in Polish, pl language, we are still able to access the url. How do you avoid this? The solution, I recommend uses Sitecore processor functionality. I've created my own processor which is executed within httpRequestBegin just after the ItemResolver processor.
</httpRequestBegin>
	...
	<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
	<processor type="Cognifide.SitecoreHelpers.ItemLaguageVersionValidator, Cognifide.SitecoreHelpers" /><!-- my processor -->
	...
</httpRequestBegin>
The code of the processor is very simple - it checks whether the current Item has any version and if there is no version, the processor assigns null to the Sitecore.Context.Item.
namespace Cognifide.SitecoreHelpers
{
    public class ItemLaguageVersionValidator : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
    {
        public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
        {
            if (Sitecore.Context.Item != null && Sitecore.Context.Item.Versions.Count == 0)
            {
                Sitecore.Context.Item = null;
            }
        }
    }
}
Now when one tries to access the page in a language that doesn't have a translated version, a proper 404 message is displayed.

I hope this post helped you. If you have any comments do not hesitate to leave your notes below. And if you have been wondering on how you can work with the Facebook Open Graph and Sitecore, you may be interested in another post from me: Sitecore Facebook Device - Facebook Open Graph device for Sitecore.

Archive