Spring Internationalization Resource Bundle Examples

Spring framework support internationalization. With this function, you can display text message in different languages to different locale request user. For example. display English text to US user and Chinese text to china user. This article will show you how to achieve this in Spring.

1. Create Different Locale Text Message Properties File.

spring internationalization resource locale files and application context bean definition file

Before coding, we should first create the text message properties file for different locale. They are all saved in src/main/resources/locale folder. And all file name prefix is message, this is called message bundle source basename.

  1. message_en_US.properties : Save message display to US Locale user.
  2. message_zh_CN.properties : Save message display to China Locale user.
  3. message_en.properties : Save message display to English Locale user.
  4. message.properties : This is the default message properties file, if no locale specified then show messages saved in it.

Content in above file are key = value pairs format like below.{0} and {1} are placeholders which will be replaced by String parameters in java codes.

welcome.info=Hello {0}, you are welcome to {1}. This is an English web site.

For east Asia language such as Chinese, you should execute C:\Java\jdk1.8.0_131\bin\native2ascii.exe to convert Chinese language to Unicode. If you use Eclipse, the editor can convert it automatically.
Content in message_zh_CN.properties:
welcome.info=\u4F60\u597D {0}, \u6B22\u8FCE\u6765\u5230 {1}. \u8FD9\u662F\u7F51\u7AD9\u4E2D\u6587\u7248\u672C.
2. Get Messages By MessageSource Interface In ApplicationContext.
org.springframework.context.MessageSource interface provide methods to get message by locale, and ApplicationContext just extends it, so we can use ApplicationContext.getMessage(String messageKey, String paramsArrp[], Locale locale) to get related messages.
  1. Create application context xml file ResourceBeanSettings.xml in src/main/resources folder. Add below bean definition, the bean id must be messageSource otherwise an Exception will be thrown. The basename should include directory path relative to src/main/resources. org.springframework.context.support.ResourceBundleMessageSource class just implement MessageSource interface. It is commonly used to retrieve different locale resource bundles messages.
        <!-- The bean id must be messageSource otherwise an Exception maybe occurred during execution. --> 
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
             <!-- This is text message properties file prefix, in this example is locale\message. --> 
             <property name="basename"> <value>locale\message</value> </property> 
        </bean>

    spring internationalization resource locale files and application context bean definition file

  2. ResourceBundleMessageSourceExample.java
    public class ResourceBundleMessageSourceExample {
    
    	public static void main(String[] args) {
    		
    	   	// Initiate Spring application context.
    		ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("ResourceBeanSettings.xml");
    		
    		/* Declare text message input parameters value. 
    		 * These String value will be inserted at {0} and {1} placeholder in text message by index. */
    		String paramsArray[] = {"Jerry","dev2qa.com"};
    		
    		/* Get US locale message. */
    		String message = springAppCtx.getMessage("welcome.info", paramsArray, Locale.US);
    		System.out.println(message);
    		
    		/* Get English locale message. */
    		message = springAppCtx.getMessage("welcome.info", paramsArray, Locale.ENGLISH);
    		System.out.println(message);
    		
    		/* Get China locale message. */
    		message = springAppCtx.getMessage("welcome.info", paramsArray, Locale.CHINA);
    		System.out.println(message);
    		
    		/* Get default locale. */
    		Locale defaultLocale = Locale.getDefault();
    		message = springAppCtx.getMessage("welcome.info", paramsArray, defaultLocale);
    		System.out.println(message);
    
    	}
    
    }
    
  3. Above code execution output.
    spring resource bundle example output

3. Get Messages In Spring Bean By Implement MessageSourceAware. 

If you want to get resource messages in Spring bean, you can make the bean implement MessageSourceAware interface. And override it’s setMessageSource(MessageSource messageSource) method. Spring framework will inject a MessageSource object into it automatically.

MessageSourceAwareExample.java

public class MessageSourceAwareExample implements MessageSourceAware {

	// This is private MessageSource object.
	private MessageSource messageSource;
	
	public MessageSource getMessageSource() {
		return messageSource;
	}

	// Spring framework inject MessageSource object automatically.
	@Override
	public void setMessageSource(MessageSource messageSource) {
		// Assign system injected object to local variable for using in other methods.
		this.messageSource = messageSource;
	}

	public static void main(String[] args) {
		
	   	// Initiate Spring application context.
		ApplicationContext springAppCtx = new ClassPathXmlApplicationContext("ResourceBeanSettings.xml");
		
		MessageSourceAwareExample messageSourceAwareExampleBean = (MessageSourceAwareExample)springAppCtx.getBean("messageSourceAwareExampleBean");
		
		/* Declare text message input parameters value. 
		 * These String value will be inserted at {0} and {1} placeholder in text message by index. */
		String paramsArray[] = {"Elon","dev2qa.com"};
		
		/* Get US locale message. */
		String message = messageSourceAwareExampleBean.getMessageSource().getMessage("welcome.info", paramsArray, Locale.US);
		System.out.println(message);
		
		/* Get English locale message. */
		message = messageSourceAwareExampleBean.getMessageSource().getMessage("welcome.info", paramsArray, Locale.ENGLISH);
		System.out.println(message);
		
		/* Get China locale message. */
		message = messageSourceAwareExampleBean.getMessageSource().getMessage("welcome.info", paramsArray, Locale.CHINA);
		System.out.println(message);
		
		/* Get default locale. */
		Locale defaultLocale = Locale.getDefault();
		message = messageSourceAwareExampleBean.getMessageSource().getMessage("welcome.info", paramsArray, defaultLocale);
		System.out.println(message);
	}

}

Download “Spring-Internationalization-Resource-Bundle-Examples.zip” Spring-Internationalization-Resource-Bundle-Examples.zip – Downloaded 82 times – 3 KB

(Visited 3,583 times, 2 visits today)
READ :   Spring MVC Request And Session Scoped Bean Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.