Last active
June 21, 2018 20:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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