Sunday, December 16, 2012

Spring : Working with MessageSource to Achieve internationalization in your Application

Spring provide the support to print locale specific message inside the application in a very elegant manner and some how it uses it dependecy Injection to achieve this feature fromm the org.springframework.context.MessageSource class.

let us figure out the way to achieve the internationalization of your application with the spring MessageSource Class.To construct this example I have created two resouce bundle one is for US_EN and another is for the Japanese.For Japanese Resource bundle I have used the unicode char(UTF-8).

Step 1:Let us create a simple java project(include the Spring jar into the classpath) as the depicted figure.

Step 2:Create the resource bundle as fallows:

for Japanese

Step 3: Create Spring.xml provided with the messageSource bean other the than already configured bean.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:xsi="" xmlns:aop=""

 <bean id="mp3ply" class="com.rajkrrsinghblogspot.Mp3Player">
  <property name="rockSongs">
    <value>Rock Song1</value>
    <value>Rock Song2</value>
    <value>Rock Song3</value>
  <property name="jazzSongs">
    <value>Rock Song1</value>
    <value>Rock Song2</value>
    <value>Rock Song1</value>
  <property name="classicSongs">
    <entry key="1" value="Classic Song1"/>
    <entry key="2" value="Classic Song2"/>

 <bean id="messageSource" class="">
  <property name="basenames">

 <context:component-scan base-package="com.rajkrrsinghblogspot" />

Step 4: Now Create Mp3Player bean specified in the spring.xml as fallows

package com.rajkrrsinghblogspot;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

public class Mp3Player {
 private List rockSongs;
 private Set jazzSongs;
 private Map classicSongs;
 private MessageSource messageSource;
 public MessageSource getMessageSource() {
  return messageSource;
 public void setMessageSource(MessageSource messageSource) {
  this.messageSource = messageSource;
 public List getRockSongs() {
  System.out.println("Rock Song List :"+rockSongs);
  return rockSongs;
 public void setRockSongs(List rockSongs) {
  this.rockSongs = rockSongs;
 public Set getJazzSongs() {
  System.out.println("I don't understand Japanese is :"+this.getMessageSource().getMessage("idontunderstand.japanese", null,"Default Message",Locale.JAPANESE));
  System.out.println(this.getMessageSource().getMessage("jazzSongSet.value", new Object[]{jazzSongs}, "default jazz songs", Locale.SIMPLIFIED_CHINESE));
  return jazzSongs;
 public void setJazzSongs(Set jazzSongs) {
  this.jazzSongs = jazzSongs;
 public Map getClassicSongs() {
  System.out.println("Classic Song Map:"+classicSongs);
  return classicSongs;
 public void setClassicSongs(Map classicSongs) {
  this.classicSongs = classicSongs;

Step 5: Now Create your Test Class to test the desired functionality

package com.rajkrrsinghblogspot;

import java.util.Locale;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;

public class Main {
 public static void main(String[] args) {
  //BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
  ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
  Mp3Player mp3p = (Mp3Player) ctx.getBean("mp3ply");
  String nameInJapanese = ctx.getMessage("", null, "Default Name", Locale.JAPANESE);
  String nameInEnglish = ctx.getMessage("name", null, "Default Name", Locale.ENGLISH);
  System.out.println("The Name of the user in Japanese is  "+nameInJapanese);
  System.out.println("The Name of the user in English is "+nameInEnglish);


Step 6: Now Run your program as java application and look at the console