Peter's Solaris Zone

Internationalization with JSTL

I was just playing around with JSTL (short for JavaServer Pages Standard Tag Library), trying to get it to display different messages on JSP pages for different languages.

This turned out to be more of a headache than I expected. I wasn't able to find a decent, simple, up-to-date introductory guide.

Beware older material. There's all sorts of stuff going on about taglibs and web.xml. That's all antique. And be careful of the uri for the taglib - there are old versions, and early access versions.

So here are the steps I needed. I'm assuming you're developing a web application, and that you're using Tomcat (5.5.12 or later, in my case).

Create the message catalog(s)

Create a text file - I call mine - and put your messages in on separate lines in the following format:

key = value

Whatever comes before the = sign is the key; whatever comes after it is the value. So, for example:

xsload.summary.title = Host Results
xsload.summary.msg = Status of host {0}
xsload.summary.about = About {0} = Events on {0}

As you can see, you can place positional parameters in the string.

For each language, you need a separate file. So for English, it would be; for French The keys are the same - just the values change. It will fall back to the unadorned if there's no match. However, I would recommend having both and (assuming your default language is English), even if they're identical - if you don't have the English one, and a client comes along asking for English first and French second, then they'll get the French one.

Place the Messages*.properties files in the WEB-INF/classes directory of your web application (which you may need to create first).

Get the jstl jar files

This is one of the things that tripped me up. I made the mistake of assuming that the S in JSTL, being standard, meant universally available. It doesn't. You have to supply the jstl files yourself.

There are two ways of doing this. You could get it from the Jakarta Taglibs site. But an easier way - given that you've got tomcat already - is to copy the 2 jar files that you need from there.

Specifically, you need the files jstl.jar and standard.jar from the webapps/jsp-examples/WEB-INF/lib directory of tomcat. Just copy these two files to your web application's WEB-INF/lib directory.

Tell your pages to use jstl

You need to tell your pages that you're going to use JSTL, and you need to tell it what the message catalog is called. So in each page you need:

<%@ taglib uri="" prefix="fmt" %>
<fmt:setBundle basename="Messages"/>

The basename value must match the base name of the message catalogs you created earlier.

Of course, normally you would have some include mechanism and put these lines and all your import statements in one file!

Display the messages

Rather than displaying the English string "Host Results", all you need to do is:

<fmt:message key="xsload.summary.title"/>

The syntax for positional parameters is a bit more verbose (especially as you can't auto-close the tags in this case), and an example is:

<fmt:message key="xsload.summary.msg">

You can put in as many parameters as you need, and they're processed in order.

Peter's Home | Zone Home