Message externalization in the Web view layer digs the various text out of view templates and keeps them centralized and manageable. It also provides a convenient launchpad for site internationalization.
Once again, this capability is a snap with Spring MVC. This example builds upon part 1: core to introduce message externalization and internationalization into the view layer.
The following changes are required:
ResourceBundleMessageSourcebean to the Spring context.
messages.propertiesinto British English in
SessionLocaleResolverbeans to the Spring context.
Spring needs to know where it will find externalized messages. This is done with Java's resource bundle functionality, encapsulated in a Spring
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages" /> </bean>
There isn't much in the way of messages in this example, but the little that is there is moved into a properties file named in the above
value1=Value 1: value2=Value 2: value3=Value 3: check1=Check 1: check2=Check 2: check3=Check 3:
This is also done in British English (or a reasonable facsimile thereof).
value1=Valueue 1: value2=Valueue 2: value3=Valueue 3: check1=Cheque 1: check2=Cheque 2: check3=Cheque 3:
Finally, a couple of beans are added to the Spring context to allow detection of a user's desire to switch languages, and the ability to store that preference in the user's session.
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang" /> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
A user simply includes the HTTP request parameter
lang=en_UK to change the language to British English.