For an application to support internationalization (i18n), it requires the capability of resolving text messages for different locales. Spring’s application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle.
MessageSource
is an interface that defines several methods for resolving messages. The ApplicationContext
interface extends this interface so that all application contexts are able to resolve text messages.
An application context delegates the message resolution to a bean with the exact name
messageSource
.ResourceBundleMessageSource
is the most commonMessageSource
implementation that resolves messages from resource bundles for different locales.
1. Resolve messages using ResourceBundleMessageSource
As an example, you can create the following resource bundle, messages_en_US.properties
, for the English language in the United States. Resource bundles will be loaded from the root of the classpath.
1.1. Create resource bundle
Create a file name messages_en_US.properties
in classpath of your spring application.
1.2. Configure ResourceBundleMessageSource bean
Now configure ResourceBundleMessageSource
class as bean name "messageSource"
. Additionally, you have to specify the base name of the resource bundles for ResourceBundleMessageSource
.
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"> <value>messages</value> </property> </bean>
2. Resource Bundle Lookup Order
- For this
messageSource
definition, if you look up a text message for the United States locale, whose preferred language is English, the resource bundlemessages_en_US.properties
, which matches both the language and country, will be considered first. - If there’s no such resource bundle or the message can’t be found, the one
messages_en.properties
that matches the language only will be considered. - If this resource bundle still can’t be found, the default
messages.properties
for all locales will be chosen finally.
3. Demo – How to use MessageSource
3.1. Fetch the message directly
Now to check if messages can be loaded or not, run this code:
public class TestSpringContext { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); String message = context.getMessage("msg.text", null, Locale.US); System.out.println(message); } }
Output:
Welcome to howtodoinjava.com
3.2. Implement MessageSourceAware interface in beans
Great, so we are able to resolve messages. BUT, here we are directly accessing ApplicationContext
so it looks easy. If we want to access messages in some bean instance then we will need to implement either the ApplicationContextAware
interface or the MessageSourceAware
interface.
Something like this:
public class EmployeeController implements MessageSourceAware { private MessageSource messageSource; public void setMessageSource(MessageSource messageSource) { this.messageSource = messageSource; } //Other code }
Now you can use this messageSource
instance to retrieve / resolve the text messages defined in resource files.
Happy Learning !!
Getting below error:
Where did you declare the messageSource bean, in the project?
spring configuration file
Thank you!
Thanks, nice tips,