How to make I18N with xsl and xml

I am trying to make a page in different languages ​​using xml / xsl. I want to have only one xml and one xsl. On my Url page I have a pLanguage parameter that I think I can use to see if I have selected English or Dutch.

I tried with this code, but I don't know how I put it together:

First, I make variables of all the words that need to be translated as follows:

<xsl:variable name="lang.pageTitle" select="'This is the title in English'"/> 

To get pageTitle in a template, I can now use

 <xsl:value-of select="$lang.pageTitle"/> 

I decided to replace the first line of code above using the if-else statement to check if my selected EN or NL language is as follows:

 <xsl:choose> <xsl:when test="$choosenLanguage &#61; 'NL'"> <xsl:variable name="lang.pageTitle" select="Titel in het nederlands'"/> </xsl:when> <xsl:otherwise> <xsl:variable name="lang.pageTitle" select="'This is the title in English'"/> </xsl:otherwise> </xsl:choose> 

But I get the error: java.lang.IllegalArgumentException: cannot parse the argument number $ lang.opdracht

+6
source share
1 answer

Here is a complete example of how this can be done in a general 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 to any XML document (not used), the desired and correct result is created (the header is created in accordance with the global / external parameter $pLang ):

 <html> <title>Titel in het nederlands</title> </html> 

Please note :

It is recommended that you save all the lines in an XML document that is separate from the file (s) of the XSLT stylesheet. This allows you to change / add / delete lines without changing XSLT code.

To access strings from another XML document, the code remains almost the same, the only difference is that the argument to the document() function is now the URI for the XML string document.

+2
source

Source: https://habr.com/ru/post/923534/


All Articles