Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

An XSLT style sheet and an XML dictionary approach to internationalization

Minimize the number of files you need to edit when the content on your site changes

Laura Menke (menkell@us.ibm.com), Technical Coordinator, IBM WebSphere Developer Domain
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.

Summary:  In this article, Laura will show you how to leverage XML and XSLT technology to enable dynamic internationalization of your Web pages through a dictionary-driven approach. Provided is a sample of a generic XSLT style sheet that you can extend or include in your applications. Also included is a sample dictionary layout that will work along with the style sheet. This approach will allow you to minimize the number of files you need to edit when the content on your site changes.

Date:  01 Apr 2001
Level:  Introductory
Also available in:   Japanese

Activity:  8805 views
Comments:  

Overview

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
Figure 1

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.


Generic XSLT style sheet

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.


Generic dictionary

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));
	.......
}


Summary

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.


Resources

  • 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.

About the author

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development
ArticleID=11526
ArticleTitle=An XSLT style sheet and an XML dictionary approach to internationalization
publish-date=04012001
author1-email=menkell@us.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers