-->

xslt localization

2020-08-24 17:29发布

问题:

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?

回答1:

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]"/>


回答2:

See my answer to this SO question, detailing how to efficiently use a lookup table stored as a separate, exteranal XML document, via <xsl:key> and the key() function.



回答3:

Text in an XSLT may be localized by reading the translated strings from an XML document. Numbers may also be localized.

The XML document may contain either one language with one XML document for each language, or alternatively, a single XML document with all languages. The XML formats in the following example follows Microsoft .NET resource (.resx) files (one file per language) or a single TMX (translation memory exchange) document with all languages. Any format, however, may be used as long as the XPath used to read the text is consistent.

Both options use the XPath 'document' function to read the XML with the translated strings. Define parameters for each string used in the XSLT. Using parameters rather than variables allows the values to be overridden when the XSLT is transformed. Use xsl:value-of to display the translated text. When the transform is processed, pass the language code, for example, 'fr', and the URL to the resource XML document for the desired language.

See my article "How to localize XSLT" for a complete, functional sample at http://www.codeproject.com/Articles/338731/LocalizeXSLT.