Skip to content

Instantly share code, notes, and snippets.

@animaux
Last active June 21, 2018 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save animaux/d86c7bca7b5e1a888549d7ae223aaa43 to your computer and use it in GitHub Desktop.
Save animaux/d86c7bca7b5e1a888549d7ae223aaa43 to your computer and use it in GitHub Desktop.
XSL function: Picks a translated string from a predefines set of variables. #symphony-cms
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://getsymphony.com/functions"
xmlns:func="http://exslt.org/functions"
xmlns:exslt="http://exslt.org/common"
xmlns:dyn="http://exslt.org/dynamic"
extension-element-prefixes="func dyn exslt a">
<!--
Picks a translated string from a predefines set of variables. Uses the first available if langauge-code is not present in variable.
USAGE
a:lang('languagestring')
TRANSLATIONS STRING VARIABLE-EXAMPLE
<xsl:variable name="languagestring">
<en>English text</en>
<de>Deutscher Text</de>
</xsl:variable>
Expects a param named `$lang` to be set! HTML inside the iso-code-nodes works too f. e. <en>English <a href="#">link</a></en>
-->
<func:function name="a:lang">
<xsl:param name="string" select="''"/>
<xsl:param name="lang" select="$lang"/>
<func:result>
<xsl:choose>
<!-- if requested langauage is NOT present … -->
<xsl:when test="dyn:evaluate(concat('not(exslt:node-set($', $string, ')/', $lang, ')'))">
<!-- … pick first language in variable -->
<xsl:for-each select="dyn:evaluate(concat('exslt:node-set($', $string, ')/*[1]'))">
<!-- match text and nodes inside the iso-code-node -->
<xsl:copy-of select="*|text()"/>
</xsl:for-each>
</xsl:when>
<!-- pick the requested language -->
<xsl:otherwise>
<xsl:for-each select="dyn:evaluate(concat('exslt:node-set($', $string, ')/', $lang))">
<!-- match text and nodes inside the iso-code-node -->
<xsl:copy-of select="*|text()"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</func:result>
</func:function>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment