March 11, 2017

#Spring part 6: Using MessageSource to get text from property files

For an application to support internationalization, 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, which 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.

First you need to create a properties file (in our case its mymessage.properties), and declare the key, value in it, e.g:
greeting=Hello

Inside spring.xml, we need to declare that property file, e.g:
< bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" >
    < property name="basenames" >
        < list > < value > mymessage < /value > < /list >
    < /property >
< /bean >


getMessage() message from AbstractApplicationContext is used to get the message from property file. getMessage method takes four parameters:String code, Object[] args, String defaultMessage, Locale locale.

  • 1st parameter: key, 
  • 2nd parameter: If the message is parametrized, then the parameter values are passed in the second array.
  • 3rd parameter: The third parameter is the default message used when the property is not found.
  • 4th parameter: The fourth indicates the locale to use.
AbstractApplicationContext  context=new ClassPathXmlApplicationContext("beanPostProcessorSpring.xml");
context.getMessage("greeting", null, "default hello!", null);


OR you can do it inside your bean by declaring a member variable of messageSource

@Autowired
private MessageSource messageSource;
/*getters and setters of messageSource*/


To print the value inside the bean:
this.messageSource.getMessage("greeting", null, "default hello!", null);

Suppose, you want to get the message for Locale.US, then whenever you call getMessage() and pass Locale.US, it will find it in the resource bundle messages_en_US.properties. 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.

Parametrized resource bundle
Declare a parametrized value in properties file, e.g:
greet.person=Hello {0}, welcome to {1}

Inside bean, while calling the getMessage(), we need to pass array of arguments in the second parameter, e.g:
this.messageSource.getMessage("greet.person", new Object[] {"Freedy", "Scrutiny Publisher"}, "default hello!", null);

It will print:
Hello Freedy, welcome to Scrutiny Publisher

-K Himaanshu Shuklaa..

No comments:

Post a Comment