O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

24.3. Creating a ResourceBundle as a Properties File

Problem

You want to store your i18n resources in your web application.

Solution

Create a text file with name/value pairs representing your i18n resources. Name the file with your global resource name and store it beneath WEB-INF.

Discussion

Adding i18n-related resources to your web application involves creating properties files or ResourceBundle classes (Recipe 24.4). A ResourceBundle that takes the form of a properties file is simply a list of keys and values, produced in any text editor. The keys represents the words that you want to be translated, and the values are the translations. These files are the resources that the web application uses to dynamically translate text into the appropriate language.

Imagine that you are creating some resources with a global name, or basename, of "WelcomeBundle." Example 24-3 shows the subclass of this resource for the visitors from the locale "es_ES," or people from Spain who speak Spanish.

For example, the key "Welcome" is associated with its Spanish equivalent "Hola y recepción." Recipe 24.5 shows how a servlet would use a ResourceBundle like this to dynamically translate "Welcome" to the visitor's language.

Example 24-3. The contents of a ResourceBundle file named WelcomeBundle_es_ES.properties

#Spanish language resources 
Welcome = Hola y recepción

These are just keys and values separated by newline characters. Comments are delineated by a hash (#) character.

This text file has to be stored in ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required