How to do I18N with xsl and xml
Here is a complete example how this can be done in a generic way:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pLang" select="'nl'"/>
<my:texts>
<pageTitle lang="en">This is the title in English</pageTitle>
<pageTitle lang="nl">Titel in het nederlands</pageTitle>
</my:texts>
<xsl:variable name="vTexts" select="document('')/*/my:texts"/>
<xsl:template match="/">
<html>
<title>
<xsl:value-of select="$vTexts/pageTitle[@lang = $pLang]"/>
</title>
</html>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the wanted, correct result (the title is generated in accordance with the global/external parameter $pLang
) is produced:
<html>
<title>Titel in het nederlands</title>
</html>
Do note:
It is recommended that all strings be maintained in an XML document that is separate from the XSLT stylesheet file(s). This allows the strings to be modified/added/deleted without changing the XSLT code.
To access the strings from another XML document the code remains almost the same, the only difference is that the argument to the document()
function now is the URI to the strings XML document.