Language fallback

16 Jan 2015, last update: 24 Jun 2017

Avoid displaying pages without a language version

This is Sitecore basic knowledge but many websites don’t have a language fallback. As described in Sitecore and the error page paragraph Sitecore Language Code in URL of querystring. It is a good idea to check if the current language is correct to avoid displaying pages without content and allow to set a not working language cookie for the website.

A solution is the use the

Language Fallback module on the marketplace.

This module is specific for multilingual items and sites. And displaying a default language for some content or ignore not translated items.

Another solutions is throw a 404 not found this is descript in the following blog:

Sitecore displaying pages without a language version

Another solutions is to redirect to the correct language. A Good solution for single language websites and multilingual sites that should be 100% translated or display empty content in case a component item is not translated (Default Sitecore behavior)

The following example don’t use a pipeline like the other solution. Advantage is that it also can use without issues when working with multiple vendors in an environment. Just at the code to your masterpage.

 

@{
    //some code to detect if context is in the correct language, this example is for single language. And required a case sensitive correct language code in the site config
    if (Sitecore.Context.Item.Language.ToString() != Sitecore.Context.Site.Language)
    {
        var url = LanguageRedirect.GetPageWithLanguageUrlOptions(Sitecore.Context.Item,Sitecore.Context.Site.Language);
        if (!string.IsNullOrEmpty(url))
        {
            Log.Info("Language Redirect To:"+url, this);
            Response.Redirect(url);
        }
    }
}
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Globalization;
using Sitecore.Links;

namespace Mirabeau.Website.Helpers
{
    public static class LanguageRedirect
    {
        public static Item GetLanguageVersion(Item item, string languageName)
        {
            var language = item.Languages.FirstOrDefault(l => l.Name == languageName);
            if (language != null)
            {
                var languageSpecificItem = global::Sitecore.Context.Database.GetItem(item.ID, language);
                if (languageSpecificItem != null && languageSpecificItem.Versions.Count > 0)
                {
                    return languageSpecificItem;
                }
            }
            return null;
        }
        public static string GetPageWithLanguageUrlOptions(Item item, string language)
        {
            var options = global::Sitecore.Links.LinkManager.GetDefaultUrlOptions();
            options.LanguageEmbedding = LanguageEmbedding.Always;
            options.Language = Language.Parse(language);
            options.EmbedLanguage(LanguageManager.GetLanguage(language));

            var newItem = item;
            if (item.Language.ToString() != language)
            {
                newItem = GetLanguageVersion(item, language);
            }
            if (newItem != null)
            {
                return LinkManager.GetItemUrl(newItem, options);
            }
            return string.Empty;
        }
    }
}