Recent Posts

Monday, 17 June 2019

Internationalization in Spring

Internationalization in Spring
     Designing java applications w.r.t Local Users is called as Internationalization. To provide Internationalization services to the users, first, we have to divide all the users into groups as per locality, for this, we have to use the following parameters.
1. language: It able to represent two lower case letters.
E.g. en, it, hi etc.

2. country: It will be represented in the form of two Upper case letters.
E.g. US, IN, IT etc.

3. System Varient [OS]: It will be represented in the form of three lower case letters.
E.g. win, uni, lin etc.
     In java applications, to represent a group of local users JAVA has provided a predefined class in the form of "java.util.Locale".
To create Locale class object we have to use the following Constructors.
public Locale(String lang)
public Locale(String lang, String country)
public Locale(String lang, String country, String sys_Varient)
E.g.
Locale l1=new Locale("en");
Locale l2=new Locale("en", "US");
Locale l3=new Locale("en", "US", "win");

     In Java applications, we are able to provide the following services as part of Internationalization.
1. Number Formations
2. Date Formations
3. Message Formations

1. Number Formations
     It can be used to represent a number w.r.t a particular Locale. It will use java.text.NumberFormat class to represent a number.
Steps
☀ Create Locale object.
☀ Create NumberFormat class object by using getInstance() Factory method.
☀ Represent Number as per the Locale by using format(-) method.

2. Date Formations
     It can be used to represent a Date w.r.t a particular Locale, for this, it will use java.text.DateFormat class.
Steps
☀ Create Locale object.
☀ Create DateFormat class object by using getDateInstance(--) Factory method.
☀ Represent Date w.r.t the Locale by using format(--) method.

3. Message Formations
     It can be used to represent messages w.r.t a particular Locale, for this, we have to use properties files and java.util.ResourceBundle class.
Steps
☀ Create properties files with all the messages in the form of key-value pairs.
Note: properties files names must be provided in the following format.
baseName_lang_country.properties
☀ Create ResourceBundle object by using getBundle(--) Factory method.
☀ Get Message from ResourceBundle object by using getString(-) method.

Example
com/ashok/spring/resources/welcome_en_US.properties
welcome = Welcome To en US Users.
com/ashok/spring /resources/welcome_it_IT.properties
welcome = Welcome To it IT Users.
package com.ashok.spring.core.internationalization;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) throws Exception {
      Locale l = new Locale("it", "IT");
      NumberFormat numFormat = NumberFormat.getInstance(l);
      System.out.println(numFormat.format(1234567.23456));
      DateFormat dateFormat = DateFormat.getDateInstance(0, l);
      System.out.println(dateFormat.format(new Date()));
      ResourceBundle resourceBundle = ResourceBundle.getBundle("com/ashok/spring/resources/wel", l);
      System.out.println(resourceBundle.getString("welcome"));
   }
}
Output
1.234.567,235
mercoledì 13 marzo 2019
Welcome To it IT Users.
     To provide Message formations in Spring applications, Spring has provided a predefined interface in the form of "org.springframework.context.MessageSource". For MessageSource interface Spring Framework has provided the following two implementation classes.
☀ org.springframework.context.support.ResourceBundleMessageSource
☀ org.springframework.context.support.ReloadableResourceBundleMessageSource
     Where ResourceBundleMessageSource is able to get messages from properties files on the basis of the provided locale.
     Where ReloadableResourceBundleMessageSource is able to get messages from both properties files and from XML files.
Steps
1. Declare properties files with the messages and with the following format for properties files names.
baseName_lang_Country.properties.

2. Declare a Bean class with MessageSource type property and the respective setter method and the required business methods.
Note: To get a message from MessageSource object we have to use the following method.
                public String getMessage(String key, Object[] place_holder_values, Locale l)
   ☀ Where "key" is key of the message defined in properties file.
   ☀ Where "Object[] " must be provided to provide values to the place holders which we defined in messages in properties files, if place holders are not existed in messages then we have to provide "null" value as Object[].
   ☀ Where Locale is able to represent the constants like US, FRANCE, IN etc. from Locale class in order to recognize the properties file.

3. Configure bean class in properties file and inject either ResourceBundleMessageSource or ReloadableResourceBundleMessageSource object as reference in Bean object and provide bease name as property for MessageSource object.

4. In Main class, in main(), get Bean object and access business method.
package com.ashok.spring.core.internationalization.beans;

import java.util.Locale;

import org.springframework.context.MessageSource;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class I18NBean {
   private MessageSource messageSource;

   public void setMessageSource(MessageSource messageSource) {
      this.messageSource = messageSource;
   }

   public void displayMessage() {
      System.out.println("Message :" + messageSource.getMessage("welcome", new Object[] { "it", "IT" }, Locale.ITALIAN));
      System.out.println("Message :" + messageSource.getMessage("welcome", new Object[] { "en", "US" }, Locale.US));
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="i18nBean"
  class="com.ashok.spring.core.internationalization.beans.I18NBean">
  <property name="messageSource"
   ref="resourceBundleMessageSource" />
 </bean>
 <bean id="resourceBundleMessageSource"
  class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename"
   value="com/ashok/spring/resources/wel2" />
 </bean>
</beans>
package com.ashok.spring.core.internationalization.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.internationalization.beans.I18NBean;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/internationalization/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      I18NBean bean = (I18NBean) context.getBean("i18nBean");
      bean.displayMessage();
   }
}
Output
Message : Welcome To it and IT User.
Message : Welcome To en and US User..

Next Tutorial : Bean Manipulations and Bean Wrappers

Previous Tutorial : Event Handling

No comments:

Post a Comment