Internationalization in Spring

Internationalization in Spring

In this tutorial, we are going to discuss Internationalization in spring framework. Internationalization or I18N is a process that makes your application adaptable to different languages and regions without engineering changes on the source code.

Internationalization in Spring

Designing the java applications w.r.t Local Users is called 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 sysVarient)

E.g

Locale l1=new Locale("en");
Locale l2=new Locale("en", "US");
Locale l3=new Locale("en", "US", "win");

In Java applications, we can provide the following services as part of Internationalization.

  1. Number Formations
  2. Date Formations
  3. Message Formations
1. Number Formations

We can use Number Formations 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

We can use Date Formations 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

We can use Message Formations 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.

Spring has provided a predefined interface in the form of “org.springframework.context.MessageSource” to provide Message formations in Spring applications. For the MessageSource interface, Spring Framework has provided the following two implementation classes.

org.springframework.context.support.ResourceBundleMessageSource
org.springframework.context.support.ReloadableResourceBundleMessageSource

Where ResourceBundleMessageSource can get messages from properties files based on the provided locale.

Where ReloadableResourceBundleMessageSource can get messages from both properties files and XML files.

Steps

1. Declare properties files with the messages and with the following format for properties files names.

baseName_lang_Country.properties.

Declare a Bean class with MessageSource type property, 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 placeholders which we defined in messages in properties files, if placeholders do not exist in messages, then we have to provide “null” value as Object[].
  • Where Locale can represent the constants like US, FRANCE, IN, etc. from the Locale class to recognize the properties file.
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.
Internationalization in Spring

Scroll to top