In my previous two recent posts, I have explained what now comes with the core Sitecore platform for Language Fallback support as well as explanations of what, how and why to use Language Fallback and Enforce Version Presence.
Now I will show you how easy it is to quickly enable these settings and start using them.
Configuration File Updates
- In a patch config file, update the site nodes for each applicable website AND THE SHELL SITE to set the enableItemLanguageFallback and/or enableFieldLanguageFallback to true. By setting these to true on the shell site, you allow content authors to view fallback within the content editor.
- You will turn on/off enforceVersionPresence at a site-wide level here as well (no need to do that on shell).
- REMEMBER: although you have turned on the functionality at a site-wide level here in a config file, you STILL need to turn on Item or Field level fallback for specific templates / fields within the Sitecore content editor as well! (see below).
- Update your index config files to enable fallback, by setting similar attributes:
- Note: this tells the index document creator to get the fallback values, if applicable, and save the fallback values directly into the search index for that language. If you don’t tell your indexes that fallback should be considered, then the crawler will not set the item or field values into the index documents and therefore your search will return no results, even though it is displaying that fallback item or text on the site in that language.
- Link Manager: you will have more than one language in your site. Depending on your domain strategy, you most likely need to make sure language is embedded 'always' in your links.
- At Verndale, we have overridden the LinkProvider so the languageEmbedding value can be set with the site definition config and therefore can be set at a site level (by default Sitecore only allows this to be set globally for the whole instance and therefore every site).
- There are a few other configuration settings you could set, but we advise to just leave these alone with default values:
- languageFallbackFieldValues (cache size per database)
Sitecore Content Editor Updates
- Add the new language(s) to Sitecore
- Go into the System/Languages folder, right click Languages and add the new language
- After it is added, update it to set the Fallback Language field to a different language
- BE CAREFUL to not create a circular reference! BAD:
- You can however have chained fallback: FINE:
- Configure the templates/fields to fallback (depending on what kind of Language Fallback you want to use)
- Item Level Fallback:
- On EACH template, you must turn on Item Level fallback by going to the Standard Values, view the Standard Fields, and in the Advanced section, click the "Enable Item Fallback" checkbox.
- This is also where you would select the checkbox to Enforce Version Presence on a particular template
- Field Level Fallback
- On EACH field of EACH template, you must turn on "Enable field level fallback" by selecting the checkbox.
- NOTE: although it is an option, do not click the 'versioned' checkbox option here, you want the SHARED field. When I talk strategy on my next blog post, I will get into the reason why.
- CONSIDERATION: Don't just do this for your user-defined templates, you also probably want to do this for the system templates for Dictionary and Versioned Media (if you decide you want to version your images/files in the media library)
Powershell to the Rescue! (if you want to do it yourself)
Right about now, you are starting to panic over the number of field checkboxes you will have to check. DON'T! This is where the power of the Sitecore Powershell Extensions Module comes in. If you don't already have it, go get it from the Sitecore Marketplace. You can use the following example to write some scripts to quickly toggle these on (or off) recursively (modify this script to do the same for Item Level fallback or Enforce Version Presence as well).
[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:e9500170-371c-4e78-9999-c63d61b3b14e:type=powershell&text=Toggle%20On%0D%0Aget-childitem%20-recurse%20%60%0D%0A%20%20%7C%20where-object%20%7B%20%24_.TemplateName%20-match%20%22Template%20field%22%7D%20%7C%20set-itemproperty%20-Name%20%22enable%20shared%20language%20fallback%22%20-Value%20%221%22%0D%0A%20Toggle%20Off%0D%0Aget-childitem%20-recurse%20%60%0D%0A%20%20%7C%20where-object%20%7B%20%24_.TemplateName%20-match%20%22Template%20field%22%7D%20%7C%20set-itemproperty%20-Name%20%22enable%20shared%20language%20fallback%22%20-Value%20%220%22%0D%0A]Better Yet, take advantage of the fact WE have done this for you already! You can install a Sitecore package that my colleague, Steve Striga, has put together that replicates that logic using the ribbon. This is available in the Marketplace now: https://marketplace.sitecore.net/Modules/L/Language_Fallback_Tools.aspx
A click of a button will cascade down through templates, checking (or unchecking) the checkboxes for Item Fallback, Field Fallback and Enforce Version Presence.
- Item Level Fallback:
- Using Field Level Fallback and need to make the fallback content Searchable in an Index? Then you need to add an initial language version in the new language(s) to every Sitecore item where applicable, otherwise Sitecore will consider the item non-existent in that language and not index the fallback content.
AGAIN, before you start imagining the pain of clicking every single item so that you can click the link to 'Add language version', you have options to automate this.
- Use the Language Migration marketplace tool: https://marketplace.sitecore.net/en/Modules/L/Language_Migration_Tool.aspx, read more about this here: https://community.sitecore.net/technical_blogs/b/elizabeth_spranzani/posts/fallback-series-post-8-language-migration-tool
- Write a powershell script similar to what is above that will loop through all items and add a version in a particular language
- Default Sitecore to create the versions when you add an item: read more about this here: https://community.sitecore.net/technical_blogs/b/elizabeth_spranzani/posts/fallback-series-post-7-defaulting-language-versions-for-fallback
Finally, a few more code updates to consider:
- Using GlassMapper? If so, you need to make sure that the Version Count logic is disabled.
You can read more about it here: http://www.glass.lu/Mapper/Sc/Documentation/VersionCountDisabler
Basically, by default, Glass will check if the language version of an item exists to prevent null errors. With fallback, it would end up not being null, but Glass never gets that far. You need to tell it to not make that check, so that the fallback version can come into play. Note that this will open up your code for possible null exceptions if the language version truly doesn't exist and there is no fallback, so you need to make sure you have the proper null checks in your code.
You can either wrap your Glass code like this (you would want to abstract this into a base method so you don't have to always remember to do so):
Or disable it for the whole application (my personal preference) in the Application_BeginRequest, making sure to dispose of it in the EndRequest. (you can grab the code for this in the above glass link).
- Make sure that any index queries you are making have language specified in it. You should pass Sitecore.Context.Language in as a parameter.
- Something along these lines:
- var query = context.GetQueryable<SearchResultItem>().Where(x => x.Language == Sitecore.Context.Language);
- Something along these lines:
- Make sure, on each page, you are specifying the meta alt lang tags, the canonical reference is output correctly, and you've thought through your xml sitemap strategy. I'll include a post that is SEO-specific later in this series.
With all of this done, language fallback should be enabled and configured for use. As you can see, it is pretty easy to quickly turn it on and there are minimal configurations. To test and make sure this is working, I have validation steps from earlier in my series that are still valid on this post: https://community.sitecore.net/technical_blogs/b/elizabeth_spranzani/posts/fallback-series-post-2-how-to-install-and-configure-alex-shyba-39-s-partial-language-fallback-module (scroll down to the section that says "Validate fallback in content editor" and "Validate fallback in front-end".