Add Multilingual Support to Your Java Web Pages | 2 | WebReference

Add Multilingual Support to Your Java Web Pages | 2


Add Multilingual Support to Your Java Web Pages [con't]

The I18nSupportServlet Class

On the server side, the I18nSupportServlet handles the GET request from the browser. It uses Java's built-in ResourceBundle support to look up the correct bundle, based on the language and country portions of the locale code (i.e. fr_CA). A lookup is then performed on the selected bundle using the provided key. Should the lookup come up empty, the key itself is returned. This helps to identify missing items in the resource bundle files at development time. Errors are sent back in quotes in order to distinguish them from valid content. I'll be handling this in a more standardized way in the next example.

Finally, the response is sent back using UTF-8 character encoding. This assures that any language will display properly so long as the browser supports it. The last line sends the response. It is important always to send something back to the browser. Otherwise, the Ajax handler will wait for a response until the request times out. Should the user navigate away from the page or close the browser during this time, it can cause an error and/or the browser to crash. Here is the code for the servlet:

The ResourceBundle Files

To keep the lookup code as simple as possible, I chose to use resource bundle files. The two files that I'm accessing here are named and The languages are different enough to easily identify which language is being served to the client.

Here is the entry for the English welcome message:

...and the French one:

The Web.xml Deployment Descriptor

The web.xml file defines each servlet and JSP page within a Web application. This file goes into the WEB-INF directory under the document root. It defines a number of parameters that are used when the Web application is deployed into the Servlet/JSP container, including the display-name, servlet name, class-name, welcome-file-list, description and servlet-mapping:

Displaying the Page in a Browser

When you've deployed the above files to your server, your project should have a structure that is similar to the following:

Figure 3. Project Folder Structure

To view the i18nSupportTestPage.jsp page in the browser, access it through the server URL using the alias that maps to it. I named mine the same to remove all ambiguity:


Here is the welcome message in French. We know that it picked it up via the resource bundle loading because it matches the value of the welcome entry in the file:

Figure 4. French Welcome Message

Now that you've got the basic framework in place, you can easily retrieve labels for any of your language and country combinations as you need them.

In the next article, I'll add a button to toggle the message, as well as add JSON support for more complex retrievals, such as those for listboxes, tabular data, multiple form fields and error information.

Have a suggestion for an article topic? Do you have a product or service that you'd like reviewed? Email it to Rob .

Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest release is called "The Rabbit of Seville". Loosely based on Rossini's The Barber of Seville overture, Rob's amazing rendition includes a full orchestra and numerous guitar tracks. It is sold on his site as a high bitrate MP3 for only $0.99 cents! Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: August 16, 2010