Skip to content

Instantly share code, notes, and snippets.

@greystate
Created April 23, 2011 20:39
Show Gist options
  • Select an option

  • Save greystate/938957 to your computer and use it in GitHub Desktop.

Select an option

Save greystate/938957 to your computer and use it in GitHub Desktop.
Code listings for "Using Dictionary Items in XSLT" (Pimp My XSLT article)
<xsl:template match="/">
<h2><xsl:value-of select="umbraco.library:GetDictionaryItem('SearchTitle')" /></h2>
<!-- do stuff -->
</xsl:template>
<xsl:template match="/">
<h2><xsl:value-of select="umbraco.library:GetDictionaryItem('SearchTitle')" /></h2>
<form>
<p>
<input type="search" placeholder="{umbraco.library:GetDictionaryItem('SearchFieldPlaceholder')}" name="q" />
<button type="submit">
<xsl:value-of select="umbraco.library:GetDictionaryItem('SearchButtonText')" />
</button>
</p>
</form>
</xsl:template>
<DictionaryItems>
<DictionaryItem key="SearchTitle">Are you lost?</DictionaryItem>
<DictionaryItem key="SearchButtonText">Find</DictionaryItem>
<DictionaryItem key="SearchFieldPlaceholder">Type some keywords here...</DictionaryItem>
</DictionaryItems>
<xsl:variable name="search-labels" select="umbraco.library:GetDictionaryItems('SearchLabels')" />
<!-- Before: -->
<h2><xsl:value-of select="umbraco.library:GetDictionaryItem('SearchTitle')" /></h2>
<!-- After: -->
<h2><xsl:value-of select="$search-labels/DictionaryItems/DictionaryItem[@key = 'SearchTitle']" /></h2>
<!-- 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" />
<xsl:template match="/">
<h2><xsl:value-of select="$labels[@key = 'SearchTitle']" /></h2>
<form>
<p>
<input type="search" placeholder="{$labels[@key = 'SearchFieldPlaceholder']}" name="q" />
<button type="submit">
<xsl:value-of select="$labels[@key = 'SearchButtonText']" />
</button>
</p>
</form>
</xsl:template>
<!--
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>
<xsl:template match="NewsItem">
<xsl:apply-templates select="newsDate" />
<!-- do stuff -->
</xsl:template>
<xsl:template match="newsDate">
<h3>
<!-- Write localized 'Date' label -->
<xsl:apply-templates select="." mode="label" />
<xsl:value-of select="concat(': ', .)" />
</h3>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment