Copied to clipboard

Flag this post as spam?

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


  • Garrett Fisher 341 posts 496 karma points
    Oct 10, 2012 @ 19:35
    Garrett Fisher
    0

    xsl:value-of -- Inline Fallback/Nullcheck Logic?

    Hi--

    I am dying to figure out a way to make a check to see if something is in the site Dictionary and if it's not then use something else.  I have a billion instances of the following logic in my XSLT code:

    <xsl:choose>
        <xsl:when test="normalize-space(umbraco.library:GetDictionaryItem(@nodeName))">
            <xsl:value-of select="umbraco.library:GetDictionaryItem(@nodeName)"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="@nodeName"/>
        </xsl:otherwise>
    </xsl:choose>

    Please tell me there is a way to do this logic inside the <xsl:value-of> tag?  Just want to fall back on the English @nodeName if no match for it is found in the dictionary and this seems awfully wordy for such a simple thing.

    Thanks,

    Garrett

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 10, 2012 @ 21:28
    Dennis Aaen
    0

    Hi Garrett,

    I will try to help you come up with my suggestion.
    If I understand your question right you're looking for a solution where you do not have to write choose construction each time you translate something.

    <xsl:variable name="translate">
        <xsl:choose>
                <xsl:when test="normalize-space(umbraco.library:GetDictionaryItem(@nodeName))">
                        <xsl:value-of select="umbraco.library:GetDictionaryItem(@nodeName)"/>
                </xsl:when>
                <xsl:otherwise>
                        <xsl:value-of select="@nodeName"/>
                </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    And you can print it this way, if I remember correctly

    <xsl:copy-of select=$translate />

    Hope my answer can help you, if I understand correctly
    /Dennis

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 11, 2012 @ 08:01
    Chriztian Steinmeier
    0

    Hi Garrett,

    I wrote an article on using Dictionary Items in XSLT - the way you do this (using @nodeName as the key) is a perfect fit for the "bonus" at the end of the article - using that, you'd be able to do this with a single line:

    <xsl:apply-templates select="@nodeName" mode="label" />

     

    Here's the condensed version of what you need from (that article):

    <!-- Grab each section of labels -->
    <xsl:variable name="frontpage-labels" select="umbraco.library:GetDictionaryItems('FrontPage')/DictionaryItems/DictionaryItem" />
    <xsl:variable name="search-labels" select="umbraco.library:GetDictionaryItems('Search')/DictionaryItems/DictionaryItem" />
    <xsl:variable name="product-labels" select="umbraco.library:GetDictionaryItems('Products')/DictionaryItems/DictionaryItem" />
    
    <!-- Join them all for easy access -->
    <xsl:variable name="labels" select="$frontpage-labels | $search-labels | $product-labels" />
    
    <!--
        Take the label key from the element's name;
        If no DictionaryItem defined for this, wrap the element's name in hash-signs,
        to signal a missing translation to the frontend.
    -->
    <xsl:template match="* | @*" mode="label">
        <xsl:value-of select="$labels[@key = name(current())]" />
        <xsl:if test="not($labels[@key = name(current())])">
            <xsl:value-of select="concat('#', name(), '#')" />
        </xsl:if>
    </xsl:template>

    /Chriztian 

  • Garrett Fisher 341 posts 496 karma points
    Oct 15, 2012 @ 22:56
    Garrett Fisher
    0

    Thank you so much for your responses, guys.  Honestly, though, these solutions just seem like MORE code to me, no?  I mean, this is happening on many, many pages.  Is this meant to be an xsl:include, I guess?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 16, 2012 @ 00:12
    Chriztian Steinmeier
    0

    Hi Garrett,

    Yes - it's an include file that enables you to use that one single apply-templates line wherever you need a translated label output.

    This way, you'll even be able to swap an entirely different translation mechanism in, because you're not hard-wired to the Umbraco Dictionary any longer (code-wise where you need the labels).

    Anyway, I guess you _could_ do these two lines everywhere, if you don't feel like trying the include solution:

    <xsl:variable name="label" select="umbraco.library:GetDictionaryItem(@nodeName)" />
    <xsl:value-of select="(@nodeName[not(normalize-space($label))] | $label)[1]" />

    /Chriztian 

Please Sign in or register to post replies

Write your reply to:

Draft