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

Author:
animaux
Version:
2.0
Release Date:
20 Jun 2019
Category:
String Operations

Description

Picks a translated string from a predefines set of strings in a variable. Uses the first available string if the desired langauge-code is not present in variable.

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">
  
    
  <!--  
        
		Version 2.0
	
		Picks a translated string from a predefines set of strings in a variable. Uses the first available string if the desired langauge-code is not present in variable.

		USAGE 
		{a:lang('languagestringname')}
		<xsl:value-of select="a:lang('languagestringname')"/>
		<xsl:copy-of select="a:lang('languagestringname')"/>

		TRANSLATIONS STRING VARIABLE-EXAMPLE
		<xsl:variable name="langs">
			<languagestringname>
				<en>English text</en>
				<de>Deutscher Text</de>
			</languagestringname>
			<…>
		</xsl:variable>

		Expects the above variable `$langs`, as well as a param defining the current langauage, named `$lang` to be defined.
		
		You can also retrieve a string in a specific language by defining a second param. e. g.: {a:lang('languagestringname', 'en')}
	 	
		HTML inside the iso-code-nodes works too f. e. `<en>English <a href="#">link</a></en>`. For nodes inside language strings you need to use `xsl:copy-of` for retrieval.

	-->
	
	<func:function name="a:lang">
		<xsl:param name="string" select="''"/>
		<xsl:param name="lang" select="$lang"/>
		
		<func:result>
			<xsl:choose>
			  <!-- if string is not defined -->
				<xsl:when test="dyn:evaluate(concat('not(exslt:node-set($langs)/', $string, ')'))">
			    <xsl:text>undefined</xsl:text>
			  </xsl:when>
			
			  <!-- if requested langauage is NOT present … -->
				<xsl:when test="dyn:evaluate(concat('not(exslt:node-set($langs)/', $string, '/', $lang, ')'))">
				  <!-- … pick first language in variable -->
			    <xsl:for-each select="dyn:evaluate(concat('exslt:node-set($langs)/', $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($langs)/', $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