
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:
- Specify fallback language for each system language dynamically.
- Item fallback in language version level.
- 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:
- 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 ):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sitecore.Data; using Sitecore.Data.Items; using Sitecore.Globalization; using Sitecore.Pipelines.GetTranslation; namespace Sitecore8Fallback { public class DictionaryFallback { public void Process(GetTranslationArgs args) { // Allow the translate fallback process for dictionary items on the live site and with no results from pervious processors if (!args.HasResult && Sitecore.Context.Site != null && Sitecore.Context.Site.Name == "website") { string _phraseValue = GetDictionaryPhraseValue(args.Key); if (!string.IsNullOrEmpty(_phraseValue)) args.Result = _phraseValue; } } private string GetDictionaryPhraseValue(string DictionryEntryKey) { Item DicRoot = Sitecore.Context.Database.GetItem(new ID("{504AE189-9F36-4C62-9767-66D73D6C3084}")); Item DicEntry = DicRoot.Axes.GetDescendants().Where(entry => entry.TemplateID == new ID("{6D1CD897-1936-4A3A-A511-289A94C2A7B1}") && entry.Name == DictionryEntryKey).FirstOrDefault(); if (DicEntry == null) return string.Empty; var item = Sitecore.Context.Database.GetItem(DicEntry.ID, Sitecore.Context.Language, Sitecore.Data.Version.Latest); if (item.Versions.GetVersionNumbers().Length > 0) { if (!string.IsNullOrEmpty(item.Fields["Phrase"].Value)) { return item.Fields["Phrase"].Value; } } Item _languageItem = Sitecore.Context.Language.GetItem(Sitecore.Context.Database); if (_languageItem == null) return string.Empty; if (_languageItem.Fields["FallbackLanguage"] == null || string.IsNullOrEmpty(_languageItem.Fields["FallbackLanguage"].Value)) return string.Empty; // there is no version in context language. Language fallbackLanguage = Language.Parse(_languageItem.Fields["FallbackLanguage"].Value.Split('-')[0]); if (fallbackLanguage == null) { return item.Fields["Phrase"].Value; } Item fallback = Sitecore.Context.Database.GetItem(DicEntry.ID, fallbackLanguage, Sitecore.Data.Version.Latest); return fallback.Fields["Phrase"].Value; } } } - 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:
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