Avoid displaying pages without a language version

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.

  • Alex Shyba

    Hi Marek,

    Thanks for the post, you are bringing up a great topic.

    I had to deal with that for the language fallback project too. What I learned is that the httprequestbegin approach would not work if you display an item from code (rendering list of news for ex) since the pipeline is not getting hit.

    I had to approach the problem at a lower level of DataEngine as a part of the PartialLanguageFallback shared source module. Will blog about it soon.

    Great job with the blog!

  • Marek Musielak

    Thanks Alex for your comment!

    Looking forward to read the post about your apporach.

  • Mvannieuwenhuijzen

    Are you sure that’s al you did, cause I end up with an error that says: System.Exception: Could not resolve type name: Poc.Helpers.ItemLanguageVersionValidator, Poc.Helpers (method: Sitecore.Configuration.Factory.CreateType(XmlNode configNode, String[] parameters, Boolean assert)).

    Of course the class is there in the helpers direcory, and no error when adding it into the web.config.

  • Mvannieuwenhuijzen

    My mistake, I got the assembly wrong. Thanks for the great script!

  • Vaibhao

    Hi,

    Very helpful post..!!

    I have one query regarding this.

    I have 5 Child Items. All having versions in English language , And in Spanish language only two of them having version. And other three don’t have it in Spanish.

    But still 5 child items are rendering.

    Can you please suggest why this is happening.

    Thanks
    Vaibhao

  • Marek Musielak

    Vaibhao, I believe it’s for the same reason as described in the post. Try to check whether the child has a version in the chosen language before rendering it.