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="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

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

 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
   <list>
    <value>ResourceBundle</value>
   </list>
  </property>
 </bean>

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


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;
 
 @Autowired
 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;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

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");
  mp3p.getRockSongs();
  mp3p.getJazzSongs();
  mp3p.getClassicSongs();
  String nameInJapanese = ctx.getMessage("name.jp", 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