First, let's set up a message bundler. The code snippet from the Spring context is as follows:
<bean id="messageSource" class= "org.springframework.context.support.
ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
Important elements to note include the following:
- class="org.springframework.context.support.ReloadableResourceBundleMessageSource": This configures a reloadable resource bundle. It supports the reloading of properties through the cacheSeconds setting.
- <property name="basename" value="classpath:messages" />: This configures the loading of properties from messages.properties and messages_{locale}.properties file. We will discuss the locale soon.
Now, let's configure a couple of property files and make them available in the src/main/resources folder.
The following code snippets show the filename and the content of each file.
The content of the message_en.properties file is shown in the following snippet:
welcome.caption=Welcome in English
The content of the message_fr.properties file is shown in the following snippet:
welcome.caption=Bienvenue - Welcome in French
We can display the message from the message bundle in a View using the spring:message tag:
<spring:message code="welcome.caption" />