Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Crosby 12 posts 33 karma points
    Apr 14, 2013 @ 00:11
    Jonathan Crosby
    0

    Razor Multilanguage DropDown

    After a long time going through lots of different forum entries and tutorials, I finally managed to piece together a little bit of Razor code to generate a multilangue dropdown. I am using a multi site structure and wanted to link relations if available in the corresponding language. The culture settings from ASP weren't formatted the way I wanted and hence I decided to create a property on my top level language folder (just below the content folder). In that property I add the language names my preferred spelling.

    If a page is not yet translated in a certain language, then I still want all available languages to be diplayed. If a page cannot be accessed via a relation then the top level page should be displayed for the chosen language.

    Here is my razor script:

    @using umbraco.MacroEngines;
    @using umbraco.cms.businesslogic.relation;
    @using umbraco.cms.businesslogic.member;
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
    var TopLanguageNode = Model.AncestorOrSelf(1);
    DynamicNode contentRoot = new DynamicNode(-1);
    RelationType relationType = RelationType.GetByAlias("relateDocumentOnCopy");
    Relation[] relations = Relation.GetRelations(Model.Id, relationType);
    }

    <select name="langselection" onchange="window.open(this.options[this.selectedIndex].value,'_top')">
    @foreach (DynamicNode LanguageRoots in contentRoot.Descendants("Lang Top Folder Alias"))
    {
    var curLanguage = LanguageRoots.GetProperty("Language Property Alias").Value.ToString();
    var curUrl = LanguageRoots.Url;

    foreach(Relation relation in relations)
    {
    int relationId = relation.Parent.Id;
    if(relationId == Model.Id)
    {
    relationId = relation.Child.Id;
    }
    var TopLangPage = Model.NodeById(relationId).AncestorOrSelf(1);

    if(curLanguage == TopLangPage.Language)
    {
    curUrl = Model.NodeById(relationId).Url;
    }
    }

    if(curLanguage == TopLanguageNode.Language)
    {
    <option value="@curUrl" selected="selected">@curLanguage</option>
    }
    else
    {
    <option value="@curUrl">@curLanguage</option>
    }
    }
    </select>

    Any feedback would be much appreciated. I used Vizioz relations to make the page relations visible in the backend.

    When copying the language folders a missing feature in my opinion is that all existing relations should be analysed when copying a language folder structure. e.g. if page a is related to page b then page b is copied to create page c, then c and a should also be related, if the relation checkbox was selected.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies