Nitty Gritty Developer Details to Configure Language Fallback
Sep 28, 2017 • 5 Minute Read • Elizabeth Spranzani, Directrice de la technologie
It's easy to quickly enable Language Fallback and Enforce Version Presence 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 our site node config and can be set at a site level.
There are a few other configuration settings you could set, but we advise to just leave these alone with default values:
- Caching.LanguageFallbackFieldValues.DefaultCacheSize
- languageFallbackFieldValues (cache size per database)
- LanguageFieldFallback.AllowVaryFallbackSettingsPerLanguage
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: en-US -> en and en-> en-us
- You can however have chained fallback: FINE: es-us - > en-US -> en
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!
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).
Toggle On
get-childitem -recurse `
| where-object { $_.TemplateName -match "Template field"} | set-itemproperty -Name "enable shared language fallback" -Value "1"
Toggle Off
get-childitem -recurse `
| where-object { $_.TemplateName -match "Template field"} | set-itemproperty -Name "enable shared language fallback" -Value "0"
Using Enforce Version Presence Or Field Level Fallback and need to make the content Index Searchable? 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?
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.
Finally, a few more code updates you may need to make:
Using GlassMapper? If so, you need to make sure that the Version Count logic is disabled. You can read more about it here.
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 basically 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 error trap as well.
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);
Make sure on each page you are specifying the meta alt lang tags, that your canonical 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, here!