Site-wise languages in Sitecore Ribbon for multisite environment

Are you working in Sitecore multisite environment and different sites are built in different languages? Then one idea should come in your mind that can't we set site-wise languages to the Page Editor's Webedit Ribbon and Content Editor's Language Gallery Form.

By default, the ribbon shows all languages defined in /sitecore/system/Languages. In our Sitecore instance, there are 40+ languages and many different culture sites. So, this idea came in our mind and implemented.

See below snap, there are total 5 languages in System.

Default Languages in Content Editor


Default Languages in Page Editor


Expected Behavior

Now, suppose, your site A uses 2 languages and site B uses other 3 languages. Then selecting item of site A, it should show 2 languages and same for site B, it should show 3 languages.

In this example, the site is built in 3 languages: En, hi-IN, de-DE. So, the ribbon should not show other languages en-GB and fr-FR. See below snap what is expected.

Solution

Prepare template to choose site-wise languages

We can have a multilist field to the Home Item of each site, where we can choose site's languages from the whole list from /sitecore/system/languages. See below screen.


Change Languages in Content Editor Gallery

Open file: \Website\sitecore\shell\Applications\Content Manager\Galleries\Languages\Gallery Languages.xml Change the code beside to your custom code. SO, update the file as below:
     <!-- comment below line -->
     <!--<CodeBeside Type="Sitecore.Shell.Applications.ContentManager.Galleries.Languages.GalleryLanguagesForm,Sitecore.Client"/>-->

     <!-- add below line -->
     <CodeBeside Type="SitecoreTactics.GalleryLanguagesForm, SitecoreTactics"/>
Code in the CodeBeside file: Create a custom class, which will be a repilca of the Sitecore.Shell.Applications.ContentManager.Galleries.Languages.GalleryLanguagesForm class. We just need to override OnLoad event in this class. So, this will be look like..
namespace SitecoreTactics
{
 public class GalleryLanguagesForm : Sitecore.Shell.Applications.ContentManager.Galleries.GalleryForm
 {
  // Other methods

   protected override void OnLoad(EventArgs e)
  {
   Assert.ArgumentNotNull(e, "e");
   base.OnLoad(e);
   if (!Context.ClientPage.IsEvent)
   {
    Item currentItem = GetCurrentItem();
    if (currentItem != null)
    {
     // Comment below line from the code
     //foreach (Language language in currentItem.Languages)

     // Instead of all languages, we will pass only selected languages
     LanguageCollection languages  = GetMySelectedLanguages();
     foreach (Language language in languages)
     {
      // Same code which is in foreach block
     }
    }
   }
  }
  
  // Other methods

 }
}

Download above sourcecode

Here is the expected output:

Change languages in Page Editor

For this, we need to update the command from Commands.config file as below:
     <!-- comment below line -->
     <!--<command name="webedit:changelanguage" type="Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguage,Sitecore.Client"/>-->


     <!-- add below line -->
     


Code in the class file: Create a custom class, which will be a repilca of the Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguage class. We just need to overrideExecute event in this class. So, this will be look like..
namespace SitecoreTactics
{
 public class ChangeLanguage : WebEditCommand
 {
  // Other methods
 
  public override void Execute(CommandContext context)
  {
   Assert.ArgumentNotNull(context, "context");
   if (context.Items.Length == 1)
   {
    Item item = context.Items[0];
    
    // Comment below line
    //LanguageCollection languages = LanguageManager.GetLanguages(item.Database);
    
    // Instead of all languages, we will pass only selected languages
    LanguageCollection languages  = GetMySelectedLanguages();
    
    SheerResponse.DisableOutput();
    Menu control = new Menu();
    foreach (Language language in languages)
    {
     string id = "L" + ShortID.NewId();
     string languageName = GetLanguageName(language.CultureInfo);
     string icon = LanguageService.GetIcon(language, item.Database);
     string click = "webedit:setlanguage(language=" + language.Name + ")";
     control.Add(id, languageName, icon, string.Empty, click, false, string.Empty, MenuItemType.Normal);
    }
    SheerResponse.EnableOutput();
    SheerResponse.ShowPopup("ChangeLanguageButton", "below", control);
   }
  }
  
  // Other methods
 }
}
Here is the expected output:


Cheers, finally we achieved site-wise selected languages for each site for our multisite Sitecore environment.

Other Language specific customizations - used for multisite environment:
Why my Sitecore Media Item created in multiple languages?
Upload Sitecore media items in selected languages

4 comments:

  1. Excellent, but I don't know how get my sorted languages to XML. Can you help me, please?

    ReplyDelete
    Replies
    1. Hi, are you talking about fetching site specific languages to xml? That you can get it from GetMySelectedLanguages() function called from above code, which I have not written here assuming it's not a big task. In this example, I have given facility to user to get languages from the MultiList, shown in 3rd image. Still let me know if you have doubt or I misunderstood your question.

      Delete
  2. Hi, yes I'm talking about fetching data into XML (sitecore/shell/Applications/Content Manager/Galleries/Languages/Gallery Languages.xml). I've similar function for get list of available site languages as in example, called from OnLoad event, but language selector in SC Content Editor is empty - displayed empty div only, so I thing that data isn't fetch to XML.

    ReplyDelete
    Replies
    1. Hi Jiri, in the code block, there is a comment "// Same code which is in foreach block " here, you have to write the same code, which is written in Sitecore.Shell.Applications.ContentManager.Galleries.GalleryLanguagesForm's foreach block. I have skipped that code due to its length.

      Still, you can download the code from https://drive.google.com/a/patelyogesh.in/file/d/0B1otw7vE3rGTWDdhTWZ5ekpZV00/view?usp=sharing. This example shows only two languages - English and German. You can customize the way you want.

      Enjoy!

      Delete