Set Content Editor Context Language

November 03rd, 2016

Sitecore Platform provides us an option to build multiple sites in a single instance. For a multi-site solution, there are scenarios where each site is configured for different languages and the versions exist only in those respective languages. One of our clients requested to have the content editor context language to be site’s default language so that they don't have to manually change using the language dropdown. In all other cases, it will default to “en”. This article outlines the steps I have taken to address this requirement.

For the purpose of this article, let’s assume we have three websites configured for the following languages

(Site Name: Primary Language | Secondary Language)

  • Website A: en-US | es-US
  • Website B: fr-CA | en-CA
  • Website C: es-CO | en-CO

Create a new setting SetDefaultLanguageInContentEditor which can be used to toggle this feature.

          Indicates whether the content editor should show the site's default language in Content Editor
          For example: Clicking on Nerium US will set the default langauge to be en-US
          Default value: true
<setting name="SetDefaultLanguageInContentEditor" value="true"></setting>

Create a new class SetDefaultLanguageForSite which inherits from Sitecore.Shell.Applications.ContentManager.ContentEditorForm


        /// Change the default language for the content editor based on the item selected within a site

        public void ChangeContentEditorLanguage(string id)
            //Set the default language only if the setting is enabled
            if (System.Convert.ToBoolean(Settings.GetSetting("SetDefaultLanguageInContentEditor")) && id.ToLower() != SiteWebRootBranchItemId.ToGuid().ToString("N"))
                Error.AssertString(id, "id", false);

                if (ShortID.IsShortID(id))
                    id = ShortID.Decode(id);

                Item item = Factory.GetDatabase(Context.ContentDatabase.ToString()).GetItem(id);

                if (item != null)
                    /*get the site root
                      For Example: /sitecore/content/Website A is the site root for Website A site*/
                    Item currentSiteRoot = /*Add your logic*/

                    /* Check to make sure the selected item root is not equal to the one located in Branch Template
                     Example: /sitecore/templates/Branches/Nerium/Nerium Site Root/Nerium $name */
                    if (currentSiteRoot != null)
                        Language primaryLanguage = /*Add your logic to get the primary language, in my case it would be en-US for website A*/;
                        var siteConfiguredLanguages = /*Add your logic to get all the languages configured for your site, for example: en-US|es-US for website A*/;

                        if (siteConfiguredLanguages.Any())
                            if (!siteConfiguredLanguages.Contains(ContentEditorDataContext.Language))
                                /* if the current language doesn't match any one of the language(s) the site is configured for,
                                  set the Content Editor context language to the primary language of the site */
                                ContentEditorDataContext.Language = primaryLanguage;
                    ContentEditorDataContext.Language = Language.Parse("en");

We need to update couple of files to allow Sitecore to make use of this new functionality.

Navigate to ~\sitecore\shell\Applications\Content Manager\Default.aspx and update the CodeBeside which refers to the new class created.

        <sc:codebeside runat="server" type="Your namespace.SetDefaultLanguageForSite, Your assembly"></sc:codebeside>

Navigate to ~\sitecore\shell\Applications\Content Manager\ContentEditor.js file and update scContentEditor.prototype.onTreeNodeClick as follows. This script will call the ChangeContentEditorLanguage method as defined in our class.

scContentEditor.prototype.onTreeNodeClick = function (sender, id) {
    sender = $(sender);

    setTimeout(function () {
        scForm.disableRequests = true;

        if (navigator.userAgent.indexOf('Trident') > 0) {
            var focusKeeper = top.document.getElementById('scIEFocusKeeper');
            if (focusKeeper) focusKeeper.focus();

        scForm.postRequest("", "", "", "LoadItem(\"" + id + "\")");
	scForm.postRequest("", "", "", "ChangeContentEditorLanguage(\"" + id + "\")");		

    }, 1);

    return false;

Save and compile your code changes.

Back in Sitecore Client, when you click on any of the sites and their respective children, the content editor language will be set to the site's primary language. 

For Example: The image below shows the home page for Website C is set to Spanish (Colombia).


When you click on any other items which are not related to the sites, they will still use the default english language.


This has been tested and verified on Sitecore 7.2 Initial Release and Sitecore Experience Platform 8.1 Update 3.

As always, please test these changes on your development environment before applying it on Production.