Using external XML document to localize XSLT output

2.6k views Asked by At

I have an XSLT stylesheet that transforms the XML data into XSL-FO (which is then rendered as a PDF using Apache FOP).

Now, I would need to localize the output to a couple of different languages. For instance, column headers for the tables produced by the XSLT need to be localized. Here's a simplified snippet of the XSLT producing a table header with column captions "Date", "Name" and "Something" :

  ...
  <fo:table>
    <fo:table-header>
      <fo:table-row>
        <fo:table-cell>
          <fo:block>Date</fo:block>
        </fo:table-cell>
        <fo:table-cell>
          <fo:block>Name</fo:block>
        </fo:table-cell>
        <fo:table-cell>
          </fo:block>Something</fo:block>
        </fo:table-cell>
      </fo:table-row>
    </fo:table-header>
    </fo:table-body>
      <xsl:apply-templates select="item"/>
    </fo:table-body>
   </fo:table>
  ...

We are already using TMX files for internationalization in other parts of the system, so that seems like a natural way to go about it. Here's a simplified example of a TMX file with a couple of strings in two languages:

<tmx version="1.4b">
  <body>
    <tu tuid="DATE">
      <tuv xml:lang="en"><seg>Date</seg></tuv>
      <tuv xml:lang="sv"><seg>Datum</seg></tuv>
    </tu>
    <tu tuid="NAME">
      <tuv xml:lang="en"><seg>Name</seg></tuv>
      <tuv xml:lang="sv"><seg>Namn</seg></tuv>
    </tu>
 </body>
</tmx>

So basically I'd like to replace the hard-coded column headers in the XSLT with references to the strings in the TMX file (using tuid attributes), so that the same XSLT could produce output in different languages.

But I can't seem to find any resources on how to use TMX files with XSLT. Examples or pointers to some good tutorials would be greatly appreciated.

1

There are 1 answers

1
Doug Domeny On BEST ANSWER

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 on "How to localize XSLT" for a complete, functional sample at http://www.codeproject.com/Articles/338731/LocalizeXSLT.