2

I have a multi-language site with a url structure like this: www.mysite/en/home.htm or www.mysite/es/home.htm (for english and spanish versions).

In this home.htm I have a table data from an xml files. The headers of this table, <th>, are into a xsl file.

I would like to dinamically change these values depending on the language that I would detect by that /es/ in the URL.

  • If URL = www.mysite/en/home.htm then
  • Description
  • Game Type
  • ....

  • If URL = www.mysite/es/home.htm than

  • Descripción
  • Tipo De Juego
  • ....

Can anybody help me? Thanks!

1 Answer 1

2

This transformation:

<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="pUrl" select="'www.mysite/es/home.htm '"/>

 <my:headings>
  <h lang="en">
    <description>Description</description>
    <gameType>Game Type</gameType>
  </h>
  <h lang="es">
    <description>Descripción</description>
    <gameType>Tipo De Juego</gameType>
  </h>
 </my:headings>

 <xsl:variable name="vHeadings" select="document('')/*/my:headings/*"/>

 <xsl:template match="/">

  <xsl:variable name="vLang" select=
    "substring-before(substring-after($pUrl, '/'), '/')"/>

     <table>
       <thead>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/description"/></td>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/gameType"/></td>
       </thead>
     </table>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used), produces the wanted headings:

<table>
   <thead>
      <td>Descripción</td>
      <td>Tipo De Juego</td>
   </thead>
</table>

Note: In a real-world app you may want to put the language-specific data in a separate XML file (or even in files -- one per language) -- in such a case you only need to slightly change the call to the document() function in this code.

UPDATE:

The OP has indicated in a comment that the use of document() is forbidden in his environment.

Here is the same solution with a slight modification to work without using document():

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pUrl" select="'www.mysite/es/home.htm '"/>

 <xsl:variable name="vrtfHeadings">
  <h lang="en">
    <description>Description</description>
    <gameType>Game Type</gameType>
  </h>
  <h lang="es">
    <description>Descripción</description>
    <gameType>Tipo De Juego</gameType>
  </h>
 </xsl:variable>

 <xsl:variable name="vHeadings" select="ext:node-set($vrtfHeadings)/*"/>

 <xsl:template match="/">

  <xsl:variable name="vLang" select=
    "substring-before(substring-after($pUrl, '/'), '/')"/>

     <table>
       <thead>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/description"/></td>
         <td><xsl:value-of select="$vHeadings[@lang=$vLang]/gameType"/></td>
       </thead>
     </table>
 </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

6 Comments

This sounds good. But ubfortunately I'm in a DNN module and I suppose I have some limitations. I get this error: "DotNetNuke.Services.Exceptions.ModuleLoadException: Execution of the 'document()' function was prohibited. Use the XsltSettings.EnableDocumentFunction property to enable it. An error occurred at (0,0). ---> System.Xml.Xsl.XslTransformException: Execution of the 'document()' function was prohibited. Use the XsltSettings.EnableDocumentFunction property to enable it. An error occurred at (0,0). at vHeadings(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)..."
@user1345999: So do what the message says: enable this setting. Another way is to place the h elements inside an xsl:variable and then convert this RTF to a regular tree using the ext:node-set() extension function.
@user1345999: I updated my answer (look at the end) with a solution that doesn't use document()
ok, no more errors, but lang don't change. <xsl:param name="pUrl" select="'www.mysite/es-es/home.htm '"/> i have label in spanish; if i have <xsl:param name="pUrl" select="'www.mysite/en-us/home.htm '"/> I have label in english. The only difference is "en-us" instead of "en" but i have changed "<xsl:variable name="vrtfHeadings"> <h lang="en-us">" as well.
Is there a way to have pUrl dynamically?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.