Monday, 10 August 2015

Dictionary Items Langauge Fallback






In this blog we will discuss the language fallback within Sitecore and with language fallback we mean that if the requested item has no version on the current context language then the system should get the item in another language "Fallback Language".






The Sitecore Language fallback module  solve this issue instead of others as the following points stated:
  1. Specify fallback language for each system language dynamically.
  2. Item fallback in language version level.
  3. Item field fallback in item field level.

 In the first point above after install the module you can go to the System/Languages/ and specify a fallback language for each language you have; And this will work automatically for the second point above as if the version of the requested item was not found the fallback code will get the item with the fallback language.

In the third point you have to enable the fallback in field level if you want the fallback to work on the field level which mean that the field value will fallback automatically to the value of the field in the fallback language.

The thing I need and the module was not cover is fallback on the dictionary items so I had to do some customization to enable the fallback and you can get that done by doing the following two steps:


  1. Add the following class to your solution code ( you will just need to update the dictionary root item Id in addition to your site name ): 
  2.  Add the following bold line to your config file

<getTranslation>
<processor type="Sitecore.Pipelines.GetTranslation.ResolveContentDatabase, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromDomain, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromFallbackDomains, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromSiteDomain, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromContextDatabase, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromCoreDatabase, Sitecore.Kernel"/>
<processor type="Sitecore8Fallback.DictionaryFallback, Sitecore8Fallback"/>
</getTranslation>


The code above will be called when you call the following line within your web site:

Sitecore.Globalization.Translate.Text("Mohammed");

Note: It is important to check your site name as we don't need text to be executed for each dictionary event if it is not used in your side like as example the content editor labels.


1 comment:

Unknown said...

You need to change lines 29 & 30
from:
Item DicEntry = DicRoot.Axes.GetDescendants().Where(entry => entry.TemplateID == new ID("{6D1CD897-1936-4A3A-A511-289A94C2A7B1}") &&
entry.Name == DictionryEntryKey).FirstOrDefault();

to:
Item DicEntry = DicRoot.Axes.GetDescendants().Where(entry => entry.TemplateID == new ID("{6D1CD897-1936-4A3A-A511-289A94C2A7B1}") &&
entry["Key"] == DictionryEntryKey).FirstOrDefault();

This way to don't rely on the dictionary item being named exactly the same as it's key

Post a Comment