Sitecore

Adding Custom Languages In Sitecore Ribbon

By September 3, 2015 No Comments

In this post I will show one amazingness from Sitecore, customizing the Sitecore Ribbon to display some predefined languages, which are more often used on the website.

This is very useful in case the language selector from the content editor contains many languages and where finding a certain locale takes some time, especially if this operation needs to be done frequently.

Usually, this is what you get when you want to switch to another locale:

languages_dropdown

Now, since the languages aren’t alphabetically ordered (I will show how to do this in a future post), finding a certain country can get annoying.

Fortunately, Sitecore is awesome enough to let us customizing it with pretty much everything we need in the Ribbon Section.

As you can see below, we have plenty of free space in the right side of the Ribbon, so why not using this to make our lives easier?

sitecore_ribbon1

sitecore_ribbon2

Now, in order to show some custom languages there, we need to do the following steps:

  • Switch to the Core database

sitecore<em>database</em>view

  • Now, we need to add a new “Chunk” item, which we would select in a desired Strip, under /sitecore/content/Applications/Content Editor/Ribbons/Chunks

add<em>custom</em>chunks

  • You would need to also create a custom Strip child and from it, select the chunk that you previously created. Add the custom strip reference under the Strip section you would want to appear in the Ribbon. I have added it under the Revisions strip, as there we also have the current language that’s selected.

add<em>custom</em>strip

  • Now you can go back to your Chunk and add some custom languages to be displayed. As you can see in the following screenshot, I’ve added some children items having the “/sitecore/templates/System/Ribbon/Large Button” template, for each language I want to show in the Ribbon.

add<em>custom</em>language

There are some things worth mentioning here: first, that you can define the icon for your locale, and secondly and more important, that you need to define a custom event that will happen on click: “custom:setcustomlanguage(language=de-AT)“.

The language is the parameter for the method, and needs to be filled with the Country Code and this is how we will send this information to the coding part of the process.

Enough with the “boring” part, now we can switch to some real action and add some “magic” code to have this working.

  • Open the “Commands.config” config file, within your Sitecore Solution. This is where the Sitecore commands are defined, and where we can add our custom one:

 

The type is the class referenced, followed by the assembly where it resides.

  • Here is the code for the class:
using Sitecore;  
using Sitecore.Data;  
using Sitecore.Data.Items;  
using Sitecore.Diagnostics;  
using Sitecore.Text;  
using Sitecore.Web.UI.Sheer;  
using System;  
using System.Collections.Specialized;  
using Sitecore.Shell.Framework.Commands;

namespace Custom.SelectLanguage  
{
    [Serializable]
    public class SelectCustomLanguage : Command
    {
        public override void Execute(CommandContext context)
        {
            Assert.ArgumentNotNull((object)context, "context");
            if (context.Items.Length != 1)
                return;
            Item obj = context.Items[0];
            NameValueCollection parameters = new NameValueCollection();
            parameters["itemuri"] = ((object)obj.Uri).ToString();
            parameters["language"] = context.Parameters["language"];
            Context.ClientPage.Start((object)this, "Run", parameters);
        }

        protected void Run(ClientPipelineArgs args)
        {
            Assert.ArgumentNotNull((object)args, "args");
            Assert.ArgumentNotNullOrEmpty((string)args.Parameters["language"], "args");
            ItemUri itemUri = ItemUri.Parse(args.Parameters["itemuri"]);
            Assert.IsNotNull((object)itemUri, typeof(ItemUri));
            Context.ClientPage.SendMessage((object)this, "item:load(id=" + (object)itemUri.ItemID + ",language=" + args.Parameters["language"] + ",version=0)");
        }
    }
}

Code explanations:

a) The class must be “Serializable” and inherit the “Command” class.
b) The overridden method “Execute” is the entering point when the user clicks on the chunk item from Sitecore Ribbon, and the “language” parameter is transmitted in the CommandContext, from where we take it to send it further to the “Run” method.
c) The “Run” method is started as a client page, and it sends the message to load the current selected item, in the locale which was clicked.

  • Compile your code and enjoy the functionality, as this is all that’s needed.

Here’s how the result would look like:

custom_languages

Hope that you have enjoyed this, “customizations are what makes us unique“.

Adrian Iorgu

Author Adrian Iorgu

Results-oriented and self-motivated lead engineer with a focus on delivering high-quality code and products in high traffic environments. Sitecore MVP 2016 & 2017

More posts by Adrian Iorgu

Leave a Reply