Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sitecore : Sort language list and show language flags
using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Shell.Applications.ContentManager.Galleries;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Web;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Web.UI.Sheer;
using Sitecore.Web.UI.XmlControls;
using Sitecore;
namespace MyNameSpace
{
public class GalleryLanguagesForm : GalleryForm
{
protected GalleryMenu Options;
protected Scrollbox Languages;
public override void HandleMessage(Message message)
{
Assert.ArgumentNotNull((object)message, nameof(message));
if (message.Name == "event:click")
return;
this.Invoke(message, true);
}
protected override void OnLoad(EventArgs e)
{
Assert.ArgumentNotNull((object)e, nameof(e));
base.OnLoad(e);
if (Context.ClientPage.IsEvent)
return;
var currentItem = GetCurrentItem();
if (currentItem == null)
return;
using (new ThreadCultureSwitcher(Context.Language.CultureInfo))
{
IEnumerable<Language> source = currentItem.Languages;
source = source.OrderBy(x => x, new LocalLanguageComparer(currentItem));
foreach (var language in source)
{
var languageItem = LanguageManager.GetLanguageItem(language, currentItem.Database);
if (languageItem == null || languageItem.Access.CanRead() && (!languageItem.Appearance.Hidden || Sitecore.Shell.UserOptions.View.ShowHiddenItems))
{
var control = ControlFactory.GetControl("Gallery.Languages.Option") as XmlControl;
Assert.IsNotNull((object)control, typeof(XmlControl));
Context.ClientPage.AddControl((System.Web.UI.Control)this.Languages, (System.Web.UI.Control)control);
var obj = currentItem.Database.GetItem(currentItem.ID, language);
if (obj != null)
{
int length = obj.Versions.GetVersionNumbers(false).Length;
string str1;
if (obj.IsFallback)
{
str1 = Translate.Text("Fallback version");
}
else
{
string str2;
if (length != 1)
str2 = Translate.Text("{0} versions.", length.ToString());
else
str2 = Translate.Text("1 version.");
str1 = str2;
}
var cultureInfo = language.CultureInfo;
control["Icon"] = GetIcon(language, currentItem.Database);
control["Header"] = cultureInfo.DisplayName + " : " + cultureInfo.NativeName;
control["Description"] = str1;
control["Click"] = string.Format("item:load(id={0},language={1},version=0)", currentItem.ID, language);
control["ClassName"] = !language.Name.Equals(WebUtil.GetQueryString("la"), StringComparison.OrdinalIgnoreCase) ? "scMenuPanelItem" : "scMenuPanelItemSelected";
}
}
}
}
var obj1 = Sitecore.Client.CoreDatabase.GetItem("/sitecore/content/Applications/Content Editor/Menues/Languages");
if (obj1 == null)
return;
this.Options.AddFromDataSource(obj1, string.Empty);
}
public static string GetIcon(Language language, Database database)
{
Assert.ArgumentNotNull(language, "language");
Assert.ArgumentNotNull(database, "database");
var icon = language.GetIcon(database);
if (string.IsNullOrEmpty(icon))
{
var languageDefinition = LanguageDefinitions.GetLanguageDefinition(language);
if (languageDefinition != null)
{
icon = languageDefinition.Icon;
}
}
if (string.IsNullOrEmpty(icon))
{
icon = "Flags/16x16/flag_generic.png";
}
return icon;
}
/// <summary>Gets the current item.</summary>
/// <returns>The current item.</returns>
private static Item GetCurrentItem()
{
var queryString1 = WebUtil.GetQueryString("db");
var queryString2 = WebUtil.GetQueryString("id");
var language = Language.Parse(WebUtil.GetQueryString("la"));
var version = Sitecore.Data.Version.Parse(WebUtil.GetQueryString("vs"));
var database = Factory.GetDatabase(queryString1);
Assert.IsNotNull((object)database, queryString1);
return database.GetItem(queryString2, language, version);
}
}
}
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.SecurityModel;
using System;
using System.Collections.Generic;
using Sitecore.Globalization;
namespace MyNameSpace
{
public class LocalLanguageComparer : Comparer<Language>
{
private readonly Item workingItem;
public LocalLanguageComparer(Item myItem)
{
Assert.ArgumentNotNull(myItem, "item");
workingItem = myItem;
}
public override int Compare(Language lang1, Language lang2)
{
Assert.ArgumentNotNull(lang1, "lang1");
Assert.ArgumentNotNull(lang2, "lang2");
if (lang1 == lang2)
{
return 0;
}
using (new SecurityDisabler())
{
var item1 = workingItem.Database.GetItem(workingItem.ID, lang1);
var item2 = workingItem.Database.GetItem(workingItem.ID, lang2);
var length1 = item1.Versions.GetVersionNumbers(false).Length;
var length2 = item2.Versions.GetVersionNumbers(false).Length;
// if both have 0 versions, then sort alphabetically.
if (length1 == 0 && length2 == 0)
{
var val1 = string.Compare(lang1.CultureInfo.DisplayName, lang2.CultureInfo.DisplayName, StringComparison.OrdinalIgnoreCase);
return val1;
}
// if one has 0 version then put those that have a version on top
if (length1 == 0 || length2 == 0)
{
var val2 = Math.Sign(length2.CompareTo(length1));
return val2;
}
// then if both have more than 0 versions then sort them alphabetically
var val3 = string.Compare(lang1.CultureInfo.DisplayName, lang2.CultureInfo.DisplayName, StringComparison.OrdinalIgnoreCase);
return val3;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.