Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt localization

I am having following xml file: -

<?xml version="1.0" encoding="UTF-8"?> 
<directory> 
   <employee> 
      <name>Joe Smith</name> 
      <phone>4-0192</phone> 
   </employee> 
   <employee> 
      <name>Sally Jones</name> 
      <phone>4-2831</phone> 
   </employee> 
</directory>

And following xslt : -

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html"/>
   <xsl:template match="directory">
      <div>List of Employee<xsl:value-of select="@directory"/>
      </div>
      <br/>
      <table>
        <tr>
          <td>Employee Name</td>
          <td>Contact Details</td>
        </tr>
        <xsl:apply-templates select="employee"></xsl:apply-templates>
      </table>

    </xsl:template>

    <xsl:template match="employee">
      <tr>
        <td>
           <xsl:value-of select="@name"/>
        </td>
        <td>
           <xsl:value-of select="@phone"/>
        </td>
      </tr>
    </xsl:template>

</xsl:stylesheet>

I would like to localize xslt text : List of Employee, Employee Name & Contact Details

How to localize the xslt text?

like image 736
Anil Purswani Avatar asked May 15 '11 07:05

Anil Purswani


1 Answers

I can see three ways to do this, which one is best (or if any of these is an alternative) depends on when and how you need the final xml:

Construct the xsl programmatically

Build the xsl using for example XmlDocument - then you can use regular string resources to fill in the labels, and possibly make use of the culture settings of your application.

Embedd the translation in the xsl

Use a <xsl:param> to tell the transform what language to use, then put a <xsl:choose> at every string:

<xsl:choose>
    <xsl:when test="$language='en'">Contact Details</xsl:when>
    <xsl:when test="$language='sv'">Kontaktuppgifter</xsl:when>
    <xsl:otherwise>Unknown language <xsl:value-of select="$language"/></xsl:otherwise>
</xsl:choose>

Look up the translations as part of the transform

Put the translations in an xml documents of its own translation.xml:

<strings>
    <string language="en" key="ContactDetails">Contact Details</string>
    <string language="sv" key="ContactDetails">Kontaktuppgifter</string>
    [...]
</strings>   

Then load it's contents with:

<xsl:variable name="strings" select="document('translation.xml')/strings"/>

...and access them with:

<xsl:value-of select="$strings/string[@key='ContactDetails' and @language=$language]"/>
like image 100
Anders Lindahl Avatar answered Sep 28 '22 07:09

Anders Lindahl