Today's reporting applications use XML extensively to format data, regardless of the different data sources. More specifically, Web-based reporting applications usually use XSL transformations to present this XML data to different clients. The standard flow for current reporting systems is more or less represented by this chain: legacy data source > XML > XSL transformation > Web browser. Note that you can perform the XSL transformation step either on the server side or the client side (the browser). The choice generally depends on nonfunctional requirements; for example, in a system with a high request rate, moving the XSL transformation to the client could result in performance and scalability benefits. The Web page coming from the XSL transformation must be internationalized. However, when the XSL transformation is performed on the client, the internationalization should be handled during the transformation itself. This means that the XSL transformation should manage the translation of the messages dynamically. In this article, we propose a general solution to address this issue.
The proposed solution consists of these building blocks:
- An XSLT client-side transformation
- Language ID availability during the transformation (such as the it_IT language code)
- An XML dictionary in UTF-8 format; the file-naming convention must contain the language ID
- The XSLT
documentfunction to access the XML dictionary
Defining the dictionary, database, and transformation
Listing 1 defines the structure for the simple XML dictionary, named dictionary_en.xml.
Listing 1. XML dictionary
<?xml version="1.0" encoding="utf-8"?> <Dictionary xml:lang="en-US"> <Label ID="TITLE"> <LabelText>Title</LabelText> </Label> <Label ID="YEAR"> <LabelText>Year</LabelText> </Label> <Label ID="AUTHOR"> <LabelText>Author</LabelText> </Label> <Label ID="HEADER"> <LabelText>Library example</LabelText> </Label> </Dictionary> |
This file contains all the labels and messages to be internationalized; each label has an ID that's referenced in the transformation and a field named LabelText that contains the string to be internationalized.
In our approach, the language ID is included in the XML file to be transformed. This means that the server determines whether this field is included in the XML. For example, if you want to internationalize with the preferred language of the requesting browser, it is enough to read the HTTP request header on the server side, search for the language ID, and include it in the XML. Listing 2 shows the XML you're going to work on.
Listing 2. XML books data
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<Books language="en">
<Book>
<Title>Being and Time</Title>
<Year>1927</Year>
<Author>Martin Heidegger</Author>
</Book>
<Book>
<Title>Shobogenzo</Title>
<Year>1231</Year>
<Author>Dogen Zenji</Author>
</Book>
</Books>
|
You want to use the XSL transformation shown in Listing 3 to display this information on the browser. Because you use a transformation on the client side, both the XSLT and the dictionary have to be available HTTP resources with a specific URI.
Listing 3. XSL transformation
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<xsl:variable name="language" select="Books/@language" />
<xsl:variable name="dictionaryName">dictionary_<xsl:value-of
select="$language"/>.xml</xsl:variable>
<xsl:variable name="dictionary" select="document($dictionaryName)" />
<xsl:template match="Books">
<html>
<head><title>Library example</title></head>
<body>
<h1>Library example</h1>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">
Title
</th>
<th align="left">
Year
</th>
<th align="left">
Author
</th>
</tr>
<xsl:for-each select="Book">
<tr>
<td><xsl:value-of select="Title"/></td>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Author"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
This produces the screen shown in Figure 1.
Figure 1. Result without internationalization

In this case, the labels are not yet internationalized.
Changing the language attribute
Listing 4 introduces the changes you need to implement to internationalize the output in Italian. The server-side application returns only the XML data that contains the XSLT reference, but it changes the language attribute to indicate the Italian locale.
Listing 4. XML books data that includes the Italian locale
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<Books language="it">
<Book>
<Title>Being and Time</Title>
<Year>1927</Year>
<Author>Martin Heidegger</Author>
</Book>
<Book>
<Title>Shobogenzo</Title>
<Year>1231</Year>
<Author>Dogen Zenji</Author>
</Book>
</Books>
|
In this way, the XSLT is able to retrieve the required language directly from XML.
As you see in Listing 5, you now need to enhance the XSLT in three different ways:
- Build the dictionary name using the language ID
- Use the
documentXSLT function to access the dictionary - Use XPath to replace the labels that have to be internationalized
Listing 5 shows what the resultant XSLT looks like.
Listing 5. Accessing the dictionary using the XSL transformation
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<xsl:variable name="language" select="Books/@language" />
<xsl:variable name="dictionaryName">dictionary_<xsl:value-of
select="$language"/>.xml</xsl:variable>
<xsl:variable name="dictionary" select="document($dictionaryName)" />
<xsl:template match="Books">
<html>
<head><title>Library example</title></head>
<body>
<h1><xsl:value-of select="$dictionary//Label[@ID='HEADER']/LabelText" /></h1>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">
<xsl:value-of select="$dictionary//Label[@ID='TITLE']/LabelText" />
</th>
<th align="left">
<xsl:value-of select="$dictionary//Label[@ID='YEAR']/LabelText" />
</th>
<th align="left">
<xsl:value-of select="$dictionary//Label[@ID='AUTHOR']/LabelText" />
</th>
</tr>
<xsl:for-each select="Book">
<tr>
<td><xsl:value-of select="Title"/></td>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Author"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
Listing 6 shows the Italian dictionary dictionary_it.xml.
Listing 6. Italian dictionary
<?xml version="1.0" encoding="utf-8"?> <Dictionary xml:lang="en-US"> <Label ID="TITLE"> <LabelText>Titolo</LabelText> </Label> <Label ID="YEAR"> <LabelText>Anno</LabelText> </Label> <Label ID="AUTHOR"> <LabelText>Autore</LabelText> </Label> <Label ID="HEADER"> <LabelText>Esempio libreria</LabelText> </Label> </Dictionary> |
This results in the screen you see in Figure 2.
Figure 2. Result with internationalization

If the required dictionary or a specific label is not available, you can use the definition of a default dictionary.
This article provided a client-side solution based on XSL transformations for internationalizing XML content. No work is required on the server; you only need to store in the XML the language to be used for internationalization. The client completes the job by using the XSLT document function to retrieve the correct dictionary in the XSL transformation.
| Description | Name | Size | Download method |
|---|---|---|---|
| Example source code | x-clientxslti18n.zip | 3KB | HTTP |
Information about download methods
Learn
- What kind of
language is XSLT? (Michael Kay, developerWorks, April 2005): Check out this analysis
and overview and learn where the language comes from, what it's good at, and why you
should use it.
- An XSLT style
sheet and an XML dictionary approach to internationalization (Laura Menke,
developerWorks, April 2001): Read this server-side approach to internationalization
and minimize how many files you need to edit when content changes on your site.
- XSLT Tutorial: Learn the
basics of XSLT and how to transform XML documents with it.
- Best Practices for XML Internationalization: Check out these best practices notes from the World Wide Web Consortium (W3C).
- IBM XML certification: Find out how you can become an IBM-Certified Developer in XML and related technologies.
- XML technical library: See the developerWorks XML Zone for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks.
- developerWorks technical events and webcasts: Stay current with technology in these sessions.
- The technology
bookstore: Browse for books on these and other technical topics.
- developerWorks
podcasts: Listen to interesting interviews and discussions for software developers.
Get products and technologies
- IBM
trial software for product evaluation: Build your next project with trial software available for download directly from developerWorks, including application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
Discuss
- XML zone discussion forums: Participate in any of several XML-related discussions.
- developerWorks XML zone: Share your thoughts: After you read this article, post your comments and thoughts in this forum. The XML zone editors moderate the forum and welcome your input.
- developerWorks blogs: Check out these blogs and get involved in the developerWorks community.

After getting his PhD in theoretical physics, Leonida Gianfagna joined IBM in 2002. He is currently working as a software engineer in IBM Tivoli Monitoring. His areas of interest include service-oriented architecture (SOA), software performance engineering, and quantum computing.

After earning a degree in computer science, Stefano Borghetti collaborated for one year with ENEA Italian Research Center and then joined IBM in 2000. He is currently working as a software engineer in the IBM Tivoli Monitoring area. His areas of interest include Web technologies, software analysis and design using visual modeling, and software performance engineering.

Antonio Perrone graduated cum laude in information sciences in 1989 at the University of Bari, Italy. He joined IBM in 1990, where he has held several positions in development and support. He has been with the IBM Tivoli division since 1996, where he is currently an architect in the network performance management area. His interests include systems management, performance and availability, knowledge representation, data integration, transaction and process modeling, and software development.
Comments (Undergoing maintenance)





