Spring i18n – ResourceBundleMessageSource

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 common MessageSource 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.

msg.text=Welcome to howtodoinjava.com

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

  1. For this messageSource definition, if you look up a text message for the United States locale, whose preferred language is English, the resource bundle messages_en_US.properties, which matches both the language and country, will be considered first.
  2. 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.
  3. 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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Spring i18n – ResourceBundleMessageSource”

  1. Getting below error:

    17:28:10.637 [main] DEBUG org.springframework.beans.factory.xml.DefaultDocumentLoader - 
    Using JAXP provider [com.sun.org.apache.xerces.internal. jaxp.DocumentBuilderFactoryImpl]
    Exception in thread "main" org.springframework.beans.factory. xml.XmlBeanDefinitionStoreException: 
    Line 1 in XML document from class path resource [applicationContext.xml] is invalid; 
    nested exception is org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 98; cvc-elt.1: 
    
    Cannot find the declaration of element 'bean'
    

    Where did you declare the messageSource bean, in the project?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.