[ Team LiB ] Previous Section Next Section

Recipe 24.3 Creating a ResourceBundle as a Properties File


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


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.


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 a place where other web components can find it, similar to installing a Java class in your web application. This is the path to the properties file, which has a fully qualified name of i18n.WelcomeBundle_es_ES.properties. The .properties extension is an essential detail!


Centralizing the i18n resources in their own WEB-INF subdirectory in a web application is a sensible way to organize this information and avoid clutter.

See Also

Recipe 24.4 on creating ResourceBundle as a Java class; the PropertyResourceBundle Javadoc: http://java.sun.com/j2se/1.4.1/docs/api/java/util/PropertyResourceBundle.html.

    [ Team LiB ] Previous Section Next Section