In Spring, you can use ResourceBundleMessageSource to resolve text messages from properties file, base on the selected locales. See following example :

1. Directory Structure

Review directory structure of this example.

directory structure of this example

2. Properties file

Create two properties files, one for English characters (, other one for Chinese characters ( Put it into the project class path (see figure above).

File : Mook Kim, age : {0}, URL : {1}

File :\ufeff\u6768\u6728\u91d1, age : {0}, URL : {1}

The ‘\ufeff\u6768\u6728\u91d1‘ is Unicode characters in Chinese.

To display the Chinese characters correctly, you have to use “native2ascii” tool to convert the Chinese characters into Unicode characters.

3. Bean configuration file

Include the properties file into the bean configuration file. Both “” and “” are consider one file in Spring, you just need to include the file name once, and Spring will find the correct locale automatically.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	<bean id="messageSource"
		<property name="basename">

P.S Assume both files are located at “resources\locale\customer\” folder.

4. Run it

package com.mkyong.common;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
public class App {
	public static void main(String[] args) {
		ApplicationContext context 
			= new ClassPathXmlApplicationContext("locale.xml");
		String name = context.getMessage("", 
				new Object[] { 28,"" }, Locale.US);
		System.out.println("Customer name (English) : " + name);
		String namechinese = context.getMessage("", 
				new Object[] {28, "" }, 
		System.out.println("Customer name (Chinese) : " + namechinese);



1. In context.getMessage(), the second argument is message parameters, you have to pass it as object array. You can just pass a null if no parameter values available.

	context.getMessage("",null, Locale.US);

2. The Locale.US will retrieve the messages from ‘‘, while Locale.SIMPLIFIED_CHINESE will retrieve the messages from ‘‘.

More …
Read this article to know how to access the MessageSource inside a bean.

Download Source Code

Tags :
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts