User's Manual

168 Chapter 13. Web Applications Tutorial
3
class. It also contains a localize(java.util.Locale) method, which is used to localize the
resource to a particular Locale.
You should use this class to represent all localizable data for your application. In Section 13.5 Glob-
alization and Bebop, you will learn how to pass GlobalizedMessages to Bebop components so that
they will perform the lookup when it is time to display the localized resource to the user.
The following example demonstrates how to access the hello_world resource defined in Proper-
tyResourceBundle. Assuming that those files are in the com.arsdigita.helloworld package,
you would create a GlobalizedMessage to represent that resource and localize it as follows:
GlobalizedMessage message = new GlobalizedMessage(
"hello_world",
"com.arsdigita.helloworld.HelloWorldResources"
);
// print in English:
System.out.println((String) message.localize(new Locale("en", "", "")));
// print in Spanish:
System.out.println((String) message.localize(new Locale("es", "", "")));
// print in French:
System.out.println((String) message.localize(new Locale("fr", "", "")));
The output will appear as:
Hello world!
Hola todo el mundo!
Salut tout le monde!
You can also make a parameter out of resources that will be interpolated using the MessageFormat
class. The next example will use the following PropertyResourceBundles:
WineResources_en.properties
this_wine_is=This is a {0} wine.
wine_color=red
WineResources_es.properties
this_wine_is=Este es un vino {0}.
wine_color=rojo
WineResources_fr.properties
this_wine_is=C’est un vin {0}.
wine_color=rouge
It is possible to create a GlobalizedMessage to perform the proper interpolation of the resource.
For example, this works if the wine_color resource is retrieved from a MessageCatalog and is not
stored statically in the PropertyResourceBundle.
GlobalizedMessage message = new GlobalizedMessage(
"this_wine_is",
"com.arsdigita.wine.WineResources",
{"wine_color"}
);
3. http://java.sun.com/j2se/1.3/docs/api/java/text/MessageFormat.html