This article assumes that the reader has a general knowledge of JSP, servlets, HTML, XML, and XSLT. In this article, I will introduce a solution for the internationlization of Web content using XSLT style sheets and an XML Dictionary.
Providing dynamic internationalization of information for display is a difficult problem. For example, an HTML page might contain buttons and labels that need to be translated to the target language. Translation of the HTML can be accomplished by coding, using a servlet, a JSP, or by using separate pages for each language. However, the source may be dynamic and these kinds of solutions can become very expensive and complicated.
Most Web applications deal with data that is formatted in XML (including HTML pages). As a result, a standard XML-based transformation mechanism (for example, XSLT) can be used to replace portions of text according to the target language. However, simply using XSLT would require one XSLT style sheet per language pairing. The solution to this problem is to create a dictionary-driven generic XSLT style sheet to control dynamic replacement of translatable portions of text. This reduces the expense and complexity of internationalizing information for display because users simply apply the generic XSLT style sheet to a new situation by providing a new dictionary. It also allows the reuse of the dictionary, creating one central place for translations of text to be stored.
Figure 1: Using a generic XSLT style sheet

Transformations of Web content can take place in many ways. You can use the generic XSLT style sheet in a servlet, JSP, or browser. For example, Internet Explorer 5.0 can handle XSLT transformations, enabling you to push some of the processing to the client for better performance. However, WAP phones and Palm Pilots do not have this capability, so in these cases the transformation must take place in a servlet or JSP.
The key to defining an XSLT style sheet is to create a generic style sheet that can be applied in many situations and isn't directly tied to a particular Web language, be it HTML, WML, or any other markup language. The generic XSLT style sheet adds the basic functionality to internationalize information for display. It can then be extended or included in other XSLT style sheets to add functionality. The generic style sheet defines the following rule: Every element in the HTML/XML document that needs be internationalized -- such as buttons, labels, or text -- must define the attribute nlsid. The value of the nlsid is used as the key into the XML dictionary. The XSLT style sheet indexes into the dictionary, finds the key that matches the value of the nlsid attribute, matches the current language in the dictionary, and retrieves the translated text. It will then replace or insert the translated text into the element body. If the style sheet cannot find the key specified by the nlsid in the dictionary, it will simply copy the contents of the element (if the element contains default contents) into the new document. Listing 1 shows a generic XSLT style sheet that implements this rule. The two parameters that are passed to the XSLT style sheet are used to define which XML dictionary to use and the preferred target language for translation.
The format of the XML Dictionary is as follows. The root element is locale. The children of locale are keys in the dictionary that can be indexed by the nlsid attribute's value. The children of the keys are the supported language with its translation of the portion of text. You may define a dictionary for each page of your Web application, each section of your Web application, or use one dictionary for you entire Web application. You may use XML dictionaries across multiple applications. Listing 2 is an example of a dictionary for a logon screen; it contains two languages: English and German.
Listing 2: Dictionary for a login screen
<?xml version='1.0'?> <locale> <userid> <en-us>User Name</en-us> <de>Benutzername</de> </userid> <password> <en-us>Password</en-us> <de>Passwort</de> </password> <logonbutton> <en-us>Logon</en-us> <de>Anmelden</de> </logonbutton> </locale> |
What your Web content looks like
As mentioned above, any element that you want to have translated needs to have an nlsid attribute defined. The element may or may not contain a default language. Listing 3a defines what a label element might look like with a default language and Listing 3b shows it without a default language. The result of the following element when the generic style sheet is applied is shown in Listing 3c. For this example, the preferred language received from the browser is German, and it uses the XML Dictionary defined above (see Listing 2).
Listing 3a: Element defined in an HTML document with default
<label id="useridLabel" nlsid="userid">User Name</label> |
or
Listing 3b: Element defined in an HTML document without a default
<label id="useridLabel" nlsid="userid"/> |
Listing 3c: Result of transform:
<label id="useridLabel" nlsid="userid">Benutzername</label> |
Servlet transformation example
Browsers such as Internet Explorer and Netscape Navigator allow you to set your preferred language. A servlet or JSP can retrieve the user's preferred language through the "Accept-Language" header of the HTTP request. The preferred language can be retrieved from within Internet Explorer by using VBScript. To internationalize your Web application, you simply pass the preferred language as a parameter to the XSLT style sheet, set the dictionary source as a parameter to the XSLT style sheet, perform the transformation on the HTML/XML document, and display the internationalized information.
For example, a servlet would contain the following code to handle the translation of an HTML page (see Listing 4).
Listing 4: Translation of an HTML page
public void doGet (HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
.......
XSLTProcessor processor =
org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor();
Processor.setStylesheetParam("currentLocale",
req.getHeader("Accept-Language"));
processor.setStylesheetParam("dictionary-file","'nls.xml'");
Processor.process(new
org.apache.xalan.xslt.XSLTInputSource("HTMLToBeTranslated.xml"),
new
org.apache.xalan.xslt.XSLTInputSource("NLSStyleSheet.xsl"),
New
org.apache.xalan.xslt.XSLTResultTarget(OutputStreamForView));
.......
} |
In this article I have shown how you can use XSLT and an XML Dictionary to create an extensible solution for internationalization of Web content. By utilizing this approach, you will reduce the amount of code that your developers have to maintain and prevent hard-coded resource bundles from being scattered throughout your applications. This approach will also allow you to have a single page source instead of the same page source written in twenty-four different languages. You can also reuse the dictionaries in a wide range of applications.
- XML.org offers a wide range of XML-related news, education, and tools.
- Mulberry Technologies, Inc. is an electronic publishing consultancy specializing in SGML- and XML-based systems.
- Get the latest version of Xalan-Java, an XSLT processor for transforming XML documents into HTML, text, or other XML document types.
- The Xerces Java Parser 1.3.1 supports XML 1.0 recommendation and contains advanced parser functionality, such as XML Schema and DOM Level 2 version 1.0.
Laura Menke works as a Technical Coordinator for IBM WebSphere Developer's Domain in Rochester, MN. She holds a degree in Computer Science from the University of Wisconsin-La Crosse. She is pursuing her Masters of Science in Computer Science at the University of Minnesota. She has been working on various object-oriented technologies for the past three years. Her main focus has been on client technologies. She can be reached at menkell@us.ibm.com.
