0 users online. Create an account or sign in to join them.Users

Author:
animaux
Version:
1.0
Release Date:
4 Apr 2019
Category:
String Operations

Description

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 ``xml English textDeutscher Text ```

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>

XSLT

View Raw
<?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>

Discuss this XSLT Utility

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details