EPiServer tab menu with multiple languages

Sometimes you want to render a menu with multiple language links, depending on the pages' lang setting, typical example is a tab menu in the master language (in this case Swedish 'sv') and the last tab in English for international visitors, the english page is only available in english and we do dont want it to be a link with the master language. And all pages comes form the same PageDataCollection, that is the problem actually, mixing cultures in the same menulist.



Tab menu markup:
<EPiServer:MenuList runat="server" id="Menu">
    <HeaderTemplate>
        <nav id="tabMenu">
            <ul>
    </HeaderTemplate>
    <ItemTemplate>
                <li><%# GetLink(Container.CurrentPage, "", "span") %></li>
    </ItemTemplate>
    <SelectedTemplate>
                <li class="current"><%# GetLink(Container.CurrentPage, "", "span") %></li>
    </SelectedTemplate>
    <FooterTemplate>
            </ul>
        </nav>
    </FooterTemplate>
</EPiServer:MenuList>

The tab menu .cs:

public partial class TabMenu : UserControlsBase
    {
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            CultureInfo ci = EPiServer.Globalization.ContentLanguage.PreferredCulture;
            EPiServer.Globalization.ContentLanguage.PreferredCulture = new System.Globalization.CultureInfo("sv");
            

            Menu.PageLink = PageReference.StartPage;
            Menu.PageLoader.GetChildrenCallback = new HierarchicalPageLoader.GetChildrenMethod(LoadChildren);
            Menu.DataBind();

            EPiServer.Globalization.ContentLanguage.PreferredCulture = ci;
        }

        /// <summary>
        /// Creates the collection for the main menu, adding the startpage
        /// </summary>
        private PageDataCollection LoadChildren(PageReference pageLink)
        {
            PageDataCollection pages = DataFactory.Instance.GetChildren(pageLink, LanguageSelector.AutoDetect(true));
            return pages;
        }

        /// <summary>
        /// Gets or sets the MenuList for this control
        /// </summary>
        public MenuList MenuList
        {
            get { return Menu; }
            set { Menu = value; }
        }

    }

The method rendering the links:



        protected string GetLink(PageData pd, string CssClass, string WrapperElement)
        {
            string ahref = @"<a href=""{0}""{1}{3}>{5}<{4}>{2}</{4}>{6}</a>";
            string title = string.Empty;
            string classe = string.Empty;
            string wrapper = "span";
            string current_element1 = string.Empty;
            string current_element2 = string.Empty;

            if (!string.IsNullOrEmpty(CssClass))
                classe = " class=\"" + CssClass + "\"";

            if (em.IsValue(pd, "TitleText"))
                title = " title=\"" + pd["TitleText"] + "\"";

            if (!string.IsNullOrEmpty(WrapperElement))
            {
                wrapper = WrapperElement;

                current_element1 = "<" + wrapper + ">";
                current_element2 = "</" + wrapper + ">";
            }

            string url = pd.LinkURL;
            PageData p2 = DataFactory.Instance.GetPage(pd.PageLink, LanguageSelector.Fallback("en", true));

            CultureInfo ci = EPiServer.Globalization.ContentLanguage.PreferredCulture;

            if (p2 != null && p2.LanguageBranch.Contains("en"))
            {
                EPiServer.Globalization.ContentLanguage.PreferredCulture = new System.Globalization.CultureInfo("en");
                url = p2.LinkURL;
                EPiServer.Globalization.ContentLanguage.PreferredCulture = ci;
            }

            return string.Format(ahref, url, title, pd.PageName, classe, wrapper, current_element1, current_element2);
        }

Inga kommentarer:

Skicka en kommentar