Sitecore: Item Fallback with Glass.Mapper

January 25, 2016 Mike Skutta

Overview
If you’re building sites with a global presence and multiple languages/regions, you may need some kind of language fallback functionality. Language fallback is the process of determining what language to serve when the content has not been translated in the requested language. In Sitecore, language fallback can occur at the item level and the field level. At the item level, if there is no version in the requested language, a fallback language version can be served instead. At the field level, if a field does not have content for the requested language, content for that field can be pulled from the fallback language version of the item.

Sitecore 8.1 now includes language fallback features such as field fallback and item fallback. Prior to Sitecore 8.1, third party modules like the Field Fallback module had to be installed to achieve language fallback functionality. This fallback can be defined at the site, template or item level. Each language can have one fallback language specified. Spanish can be set up to always fallback to English. Catalan, a language of northeastern Spain, can be set up to fallback to Spanish. If a user requested an item in Catalan, the language fallback would be Catalan > Spanish > English. Language fallback takes the path defined on each language.

There may be cases in which item language fallbacks should not take the same path. One example could be for websites targeted at a Canadian audience. In Canada, the two official languages are English and French. Quebec’s official language is French whereas Ontario’s official language is English. Many Canadians still speak their immigrant language at home while English and French is spoken outside the home. Someone with a mother tongue of Spanish might prefer to view content in Spanish. If they live in Quebec, they may prefer falling back to French if a Spanish version does not exist. In Ontario, they may prefer falling back to English.

One way we can achieve falling back to a language based on business rules is through the use of Glass.Mapper. There are other ways to achieve this, but I will be specifically focusing on Glass.Mapper in this blog post.

Custom Fallback
In the Canadian example, item language fallback is based on the context of the user and other business rules. We need the ability to specify the fallback rules on a per-use basis. Something like the following would give us the most flexibility:

Each time we need a specific fallback language, we can create a new “LanguageItemFallback” context with the language(s) that fallback. If the current item does not exist in the context language, the languages specified in the context will be used as the fallback language.

We can create the LanguageItemFallback context with the following code:

The context takes in a LanguageItemFallbackState object. This state holds an array of languages to fall through. Either the state object can be passed into the constructor or the array of languages. If you noticed, we are also creating a new VersionCountDisabler context. In order for Glass.Mapper to allow items with missing language versions through its pipeline, we need to disable the version count.

Here is the LanguageItemFallbackState used by the LanguageItemFallback:

Now that we have the LanguageItemFallback context, we need to create a task to plug into the Glass.Mapper pipeline. The task takes the array of fallback languages from the LanguageItemFallback context and performs the fallback. It also makes sure the custom fallback does not interfere with the normal Sitecore processing, including the Page Editor.

Now that we have our task created, we need to register it in the Glass.Mapper pipeline.

Usage
To use the custom language fallback, all we need to do is wrap any Glass.Mapper calls with the LanguageItemFallback context passing the list of languages. Here is an example of falling back from Catalan to Spanish to English where Catalan is the context language:

The array of languages can be changed based on the business requirements.

Summary
As you can see, it is pretty easy to create a custom item language fallback based on business rules using Glass.Mapper. Good luck!

Subscribe

The One North Ideas Update delivers each month’s latest posts on digital for PSOs—including industry trends, news and our latest research—directly to your inbox. Although it’s our goal to always include thought-provoking and compelling content, you can unsubscribe at any time. 

See our Privacy Policy to learn more about how we protect and manage your submitted data.

Mike Skutta Architect

As an Architect within the Technology Labs group at One North, Mike works to research, test and integrate new technologies for both One North and its clients. He is also a Sitecore Technology MVP – a distinction received by only 217 people worldwide – and holds more than 14 years of experience architecting, designing, developing software and leading technology teams. Based in Florida, Mike is often seen wheeling around the office via our robot.

  • Favorite vending machine snack: Doritos
  • Most unusual job: Working as a Serf at Medieval Times

One North Interactive 
222 North LaSalle St, #1500
Chicago, IL 60601

+1 312.469.1740