Copied to clipboard

Flag this post as spam?

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


  • Rasmus Berntsen 215 posts 253 karma points c-trib
    Aug 26, 2011 @ 11:41
    Rasmus Berntsen
    0

    Dictionary Datatype and Razor

    Does anybody have an example of how to use the Dictionary Datatype in Razor?

    I'm using the datatype on a Media Item and I can get a copy of the xml with: 

    getProperty("additionalNaming").Value.ToString()

    But how do I filter on the current culture?

    Razor is still pretty new to me, so might be an easy one... :)

     

    Thanks in advance!

     

  • Rasmus Berntsen 215 posts 253 karma points c-trib
    Aug 26, 2011 @ 11:49
    Rasmus Berntsen
    0

    Ohhh... 

    Response.Write(productMedia.getProperty("additionalNaming").Value.ToString());

    Outputs:

    <arrayofvalue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <value xml:lang="en-US" id="1">en</value>
    <value xml:lang="da-DK" id="2">da</value>
    <value xml:lang="de-DE" id="3">de</value>
    </arrayofvalue>

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 26, 2011 @ 11:53
    Dirk De Grave
    1

    if you're using v4.7, you'll have to parse the xml returned from your property and use linq 2 xml to fetch the required data for a specific culture. 4.7.1 should have support for syntax similar to this example posted by Sebastiaan.

    https://gist.github.com/1170848

     

    Cheers,

    /Dirk

  • Rasmus Berntsen 215 posts 253 karma points c-trib
    Aug 26, 2011 @ 11:59
    Rasmus Berntsen
    0

    I am using 4.7.0, will give it a shot. Hoped for an easy solution. :)

  • Rasmus Berntsen 215 posts 253 karma points c-trib
    Aug 26, 2011 @ 13:38
    Rasmus Berntsen
    0

    I came up with this solution if anybody else needs it:

    XElement xmlParser = XElement.Parse(productMedia.getProperty("additionalNaming").Value.ToString());
    IEnumerable query = from array in xmlParser.Elements("value") select array;
    string cultureName = System.Threading.Thread.CurrentThread.CurrentCulture.Name;


    foreach (XElement rep in query)
    {
    if (cultureName == rep.FirstAttribute.Value.ToString())
    {
    //Found current culture and matching attribute culture...
    }
    }

    Thanks for helping me in the right direction, Dirk!

  • Gaitano Boeraeve 17 posts 56 karma points
    May 10, 2012 @ 12:05
    Gaitano Boeraeve
    0

    The problem with this, for me, was that if the dictionary fields are not filled in this brings problems. (I'm using Umbraco 4.7.1.1)
    Your value is something like this then: <value xml:lang="nl-BE" id="2" /> so, not quite usefull.

    I used this dictionary datatype to give my images a culture-related caption (for in alt message or image caption in galleries)
    After working with the ImageXML I wrote a quite usefull helper. It returns the correct dictionary item if it's filled in, otherwise it returns the node name of the Image.

    public static string GetImageDictionaryCaption(int imageId)
    {
        /* VARIABLES */
        string imageXML = library.GetMedia(imageId, false).Current.InnerXml.ToString();
        string currentCulture = System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag;
    
        /* PARSE AND READ XML*/
        XDocument xDoc = XDocument.Parse(imageXML);
        string captionValue = xDoc.Descendants("value").Where(x => x.Attribute(XNamespace.Xml + "lang").Value == currentCulture).First().Value;
        string nodeName = xDoc.Root.Attribute("nodeName").Value;
    
        return captionValue == "" ? nodeName : captionValue;
    }

     

Please Sign in or register to post replies

Write your reply to:

Draft