1

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.

Rolf
  • 2,178
  • 4
  • 18
  • 29
  • Sounds like an XML format for translations. Can you give examples how these look like in you case? I would think you'd best access them as external files using the document function, retrieving the data you need as you go.. – grtjn Jan 25 '12 at 15:49
  • Can you give a snippet of your XSLT that produces column headers? It might be easier to demonstrate how to expand what you're currently doing to include the data in the TMX file. Also, if possible fragment of the TMX file with the relevant data would be useful; it's not a standard many are familiar with. – Flynn1179 Jan 25 '12 at 16:02
  • Flynn1179 & grtjn, thanks for replying. See edited post for more details. – Rolf Jan 25 '12 at 21:03

1 Answers1

5

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.

Doug Domeny
  • 4,410
  • 2
  • 33
  • 49
  • Excellent tutorial, Doug. Haven't tried it out yet, but it looks like exactly what I was looking for. – Rolf Feb 17 '12 at 10:47