Multilingual in Sitecore (Part 1): Enable Multiple Languages and Language Fall-back



Sitecore has the native capability to present content in multiple languages and can maintain multiple versions of content in each language. 

To get started, we first need to install the required language. Log into Sitecore and go to the control panel. Under Localization, click Add a new language.



From the dropdown, choose the language you want to add. Notice there are multiple options for Spanish. You can create content based on the language and the region.

Let’s see a demo; we will add Spanish as an additional language.

  1. Go to Sitecore >> Content Editor.
  2. Navigate to Languages (/sitecore/system/Languages)
  3. Right Click >> Insert >> Language.

    1
  4. Click on the drop-down and select Spanish from Spain language.

    2
  5. Click Next >> Next >>Next >> Close.

    3
  6. Publish this language.
  7. Navigate to the Home item (/Sitecore/content/Home)
  8. Click on the Language version button >> Click on the Spanish option language.
    4
  9. Click on Add new version.
    5
  10. Enter the data in the Title and Text field.

    6
  11. Publish the Home item. Remember to check if the Spanish checkbox is checked.

    7
  12. Once the publish operation is completed.
  13. Browse your local instance, in my case http://sitecoredemo.sc/

    8
  14. Now to browse Spanish site, browse http://sitecoredemo.sc/es-ES

    9

Our multi-lingual setup is done, it is very simple and with no code involved. 

There are a couple of things we need to understand is how the language read by Sitecore changes the content.

  1. Sitecore looks for language query string (?sc_lang=en)
  2. Language embedded in the URL (http://sitecoredemo.sc/es-ES)
  3. If both the above are missing then it checks for the Language cookie managed by Sitecore for the context language.

  • Recommended and SEO-friendly is the second approach to embed the language in the URL. 

  • We need to design the language selector module that can be part of the Header which will give your end-users an idea that the website is multi-lingual and they can access the site in whichever language they wish to.

Language Fall-back:



  • This first time you publish content in Spanish and view the page, you will likely get the yellow error screen. 
  • Sitecore attempts to get the Spanish version for every content item on the page (header, nav items, subnavs, content, footer, footer links, etc). 
  • If there is no Spanish version of a given piece of content, it returns a null object to the view causing this error. 
  • Though, if every view and Sitecore item (folders and properties) handled null objects correctly, you could end up with a page that displays only the translated content.




  • To correct this error we must enable language fallback. This feature was fully introduced in Sitecore 8.1. 

  • It allows you to set a default language for your site as well as a fallback language for any installed language. 

  • If there is no version for the requested language, Sitecore will look for the version in the fallback language.  

  • Select the desired fallback language for each installed language using the content editor. These can be nested (ie es-MX can fall back to "es" which can fall back to "en").




  • Next, we need to patch our site configuration to allow language fallback.  Add this patch to a file that will load late in the process according to the naming conventions and structure of the configs for your site.
      
  • Note there are two types of fallback, item level, and field level.  I found it easier to fall back at the item level because the version of the item either exists or does not exist in the chosen language.  

  • Remember to recycle the app pool after patching the configuration.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<sites>
<site name="website">
<patch:attribute name="enableItemLanguageFallback">true</patch:attribute>
</site>
</sites>
</sitecore>
</configuration>


  • Finally, must also enable item fall back on the standard values of our data templates. You can do this manually, but quickly becomes a tedious endeavor for a large number of templates.




  • We can do this quickly by using a simple PowerShell script.  

  • This script looks at every item in the list of GUID and their children, creates the standard values item if it does not exist, then sets the enabled item fall back property to true.  

  • You can modify this script to include another other template folder guids related to your site (line 5).

$guids = @()
$guids += "{B29EE504-861C-492F-95A3-0D890B6FCA09}" #templates/userdefined
$guids += "{97D75760-CF8B-4740-810B-7727B564EF4D}" #templates/system/property
$guids += "{2E5892C5-A529-4646-989B-1F15DE10453E}" #templates/common
#-Line5-Add any other guids for folders where your template items are located
foreach($guid in $guids)
{
Write-Host "Creating Standard Values items for ${guid} :" -ForegroundColor green
$Items = Get-ChildItem master: -ID ${guid} -Recurse | Where-Object {$_.TemplateName -eq 'Template'-and !$_.Children["__Standard Values"] }
foreach($Item in $Items)
{
Write-Host $Item.Paths.Path
$standardvalues = New-Item -Parent $Item -Name "__Standard Values" -type $Item.ID
$Item.Editing.BeginEdit()
$Item["__Standard values"] = $standardvalues.ID
$Item.Editing.EndEdit()
}
Write-Host "Total number of items modified: " $Items.Count
Write-Host "Enabling Item Level Fallback for ${guid} :" -ForegroundColor green
$Items = Get-ChildItem master: -ID ${guid} -Recurse | Where-Object {$_.Name -eq '__Standard Values' }
foreach($Item in $Items)
{
Write-Host $Item.Paths.Path
$Item.Editing.BeginEdit()
$Item["__Enable item fallback"] = 1
$Item.Editing.EndEdit()
}
Write-Host "Total number of items modified: " $Items.Count
}

  • To run this script, install PowerShell extensions if you don’t already have them.  Then go to the launchpad and select PowerShell ISE.



In the white box, paste the above script and select execute.



  • After the PowerShell script has been completed, make sure to publish the site to ensure all instances of the templates receive the standard values. 

  • When you view the Spanish version of a page, any content that is not created in Spanish will fall back and display the English version.  




 

 

  • Remember that installing PowerShell extensions may not be desirable in a production-level environment.  

  • You can use TDS, Unicorn, or any other content serialization tool to move template updates between your environments.

Be sure to follow me to get notices about updates!  Thanks for reading,

🙂

Comments

Post a Comment