Sitecore strange language switching

The other day we started experiencing a strange issue on one of our test sites. Pages were starting to error and looking at the logs the errors were happening in view’s which hadn’t been updated in a long time. Some of these seemed like the code wasn’t robust enough to handle when a datasource hadn’t been set (the classic object not set to an instance of an object error), but fixing these just resulted in an error in another. It also didn’t explain why this suddenly started happening, not to mention the components in question should have all had data.

Then we noticed that on the first view of any page of the site with a new session the site would display fine even though these were server errors happening. Navigating to any other page or refreshing though caused the server error to return.

This led us to look at the differences between the request headers and we had our answer. On all subsequent requests the language context had been changed to some random thing which there is no content for. The site itself is not a multilanguage site and only has content for en. Adding the language code into the URL would force the context language back and the page would work again.

How language works in Sitecore

To understand what is happening it’s good to know how languages in Sitecore work.

Sitecore is built as a platform which can server content in multiple languages. By default you start with one language in the editor (en) but are able to add more. You can read my blog post from a few years ago on how to do this here (https://himynameistim.com/2015/06/30/sitecore-adding-languages-for-a-multilingual-site/).

Sitecore will recognise which language should be displayed based on a language code in a URL. e.g. himynameistim.com/en-gb/ for English – Great Britian. This is done through the strip language pipeline which picks up these languages and sets the context language.

Config for the link manager then controls if links are generated with these language codes in the URL or not. On a single language site you would have this set to never resulting in URL’s without a language code and the default language will be used.

The flaw in all of this though is the strip language pipeline always runs, even when your pages only have one lauange. The pipeline also doesn’t check if the language it finds in the url is set up as a language on the site, so quite a lot of two letter combinations will work and change the language context. When this is changed, it is changed for the users session meaning it is possible for a url to inadvertedly cause the language context to change for a user on the site when the site only has one language.

Disabling Strip Language

As the site in question only has one language the fix is quite simple. For multilingual sites the solution is a bit harder.

For a single language site you can simply turn off the strip language functionality. You can do this using a patch config file as follows:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"&gt;
 <sitecore&gt;
   <settings&gt;
    <setting name="Languages.AlwaysStripLanguage"&gt;
       <patch:attribute name="value"&gt;false</patch:attribute&gt;
    </setting&gt;
   </settings&gt;
 </sitecore&gt;
</configuration&gt;

Item and Field Names in Sitecore

Item and Field names in Sitecore might sound like a simple subject, but there are a few options that you may not be aware of.

Item Names

When you create an item in Sitecore it will ask you for a name to give the item. That name then appears in the content tree and at the top of the details pane to identify what your looking at.

Item Name Content Tree

It’s also used for the URL in the front end of your site.

Item Name Page URL

All fairly basic stuff. However in this example you will see that I’ve placed a hyphen between “Company” and “News” to make the the URL a little more friendly, rather than having a space or no gap at all. While good for the URL it comes at a sacrifice to the admin experience where a gap would be nicer.

On a multilingual site the URL’s are also still using this same name. If your languages are English and US English that may be ok, but if your second language is French then they would probably prefer a URL in French.

Display Names

This is where Display Names come in. By setting a Display Name for your item you can configure a name to show in the admin that contains the space which can also differ per language.

Item Name Display Name

However at this point the URL on the front end of your site will still be “Company News”. Through a config setting though you can set Sitecore to use item display names rather than the item name when constructing a URL.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
  <sitecore> 
    <linkManager> 
      <providers> 
        <add name="sitecore"> 
          <patch:attribute name="useDisplayName">true</patch:attribute> 
        </add> 
      </providers> 
    </linkManager> 
  </sitecore> 
</configuration>

Of course at this point your URL will be back to having a space within it.

Field Names

When you define a template in Sitecore you create add a section name and then add a field within that section. All simple stuff, however what you generally find is you have to prefix the name of your fields with the name of the section. This is because when it comes to accessing the field data in code, the sections don’t exist and you need a way to avoid having two fields just called “Title” or “Text”.

Field Name Sections

While this solves the issue from a code perspective, for content editors it’s far from ideal.

Fortunately there is a solution to this. By navigating to the actual field item within the content tree, we can specify a Title for the field that will be used in the editor rather than its name.

Field Name Title

On top of that if you click the Configure tab and select Help, you can enter some help text that will appear next to the field name in the content editor.

Field Name Help Text

Field Name With Display and Help