Sitecore dictionary items language fallback issues.

A few weeks ago I ran into an issue in which dictionary items were displaying the key instead of the value for items that did not have a version in a specific language. I was using Sitecore 8.1 Update 3 and item language fallback was enabled and correctly set up. When I visited a site in other language than the default, the dictionary items would sometimes render the key, and sometimes they would render the value. Language fallback was working inconsistently.

To fix this, I read a lot of posts and most of them recommended resetting the translation cache by doing something similar to this:

Sitecore.Globalization.Translate.ResetCache(true)

That approach didn't work in my case. What I ended up doing was adding a processor to the GetTranslation pipeline as Steven Karrmann recommended in the community forums. To accomplish this, you need to add a patch containing the new processor:


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getTranslation>
        <processor type="XXX.XXX.Processors.GetTextWithItemFallback, XXX.XXX" patch:after="*[last()]"/>
      </getTranslation>
    </pipelines>
  </sitecore>
</configuration>

With this patch we are telling Sitecore to add a new processor at the last position in the GetTranslation pipeline. After doing this, we are now able to use the processor. The new processor should look something like this:


namespace XXX.XXX.Processors
{
    class GetTextWithItemFallback
    {
        public void Process(GetTranslationArgs args)
        {
            if (args.HasResult)
            {
                return;
            }

            if (Context.Site != null && !Common.Constants.ExcludedSites.Contains(Context.Site.Name))
            {
                Database database = args.Options.Database ?? args.ContentDatabase;
                Language fallbackLanguage = LanguageFallbackManager.GetFallbackLanguage(args.Language, database);
                if (fallbackLanguage != null)
                {
                    args.Result = Translate.TextByLanguage(args.DomainName, args.Options, args.Key, fallbackLanguage, null, args.Parameters);
                }
            }
        }
    }
}


After using this approach, dictionary items will consistently show the values instead of the keys.

Random tip: If no value is set in a dictionary item, the key will be rendered. That is Sitecore's OOTB behavior. If you don't want to show the key when an empty value is present, a quick and dirty solution is to enter an empty space as the value and that will hide the key.

Comments