Listing 7-19.MessageSource Bean Definition
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
In the preceding example, a base properties file called messages.propertieswill be located
in the classpath and used by the MessageSourcebean. Various locale-specific files will be used if
present such as messages_es.propertiesfor values in Spanish. Listings 7-20 and 7-21 show a
snippet from a MessageSourceproperties file using two different languages.
Listing 7-20.Base messages.properties File in the Classpath
homepage.welcome=Hello, welcome.
subpage.title=Books
homepage.footer=© ACME Corp. 2005
Listing 7-21.messages_es.properties File in the Classpath
homepage.welcome=Hola, bienvenidos.
subpage.title=Libros
When messages are resolved from the MessageSourcebean using the preceding resource
bundles, the specified key is looked up in the correct bundle depending on the resolved locale.
If the key is not found in the locale-specific bundle, the default bundle is searched instead. In
the preceding examples, the text for homepage.footerwill appear exactly the same in both
English and Spanish locales, as it is not defined in the messages_es.propertiesfile.
View Resolution
ResourceBundleViewResolverthat we examined earlier fully supports i18n via the standard
resource bundle behavior. By default, your views are defined in a file named views.properties
in the root of the classpath—although this can be configured. If you wish the resolver to return
different views per locale, you can define them in specific properties files, such as
views_fr.properties for French.
If you elect to supply different files for one or more locales, then you can override one or
more definitions from the basename file (views.properties), and the locale-specific selection
will fall back to looking in the default bundle, should a Viewnot exist on its own.
The principal advantage of this is being able to specify very different views for different
locales—for example, by using a completely different JSP or Velocity template. If all you need
to do is modify some or all of the text in a page, you may be better serving the same Viewto
users in all locales and relying on the message source features instead.
220 CHAPTER 7 ■THE VIEW LAYER