I18N Applications

Spring has provided support for I18N (Internationalization) applications as part of application context. To support I18N applications, we can make use of an interface called "MessageSource". This interface is implemented by the following three classes.
  • MessageSourceResourceBundle
  • ReloadableResourceBundleMessageSource
  • StaticMessageSource
First create a properties file under src as shown below. File: resource_en_us.properties
firstName=Ram
lastName=Akunuru

Next create applicationContext.xml file under src. The file is given below.
<?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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename" value="resource_en_us" />
 </bean>
 
</beans>
Next create a main class under src as shown. File: com.ram.app.I18NApp.java
package com.ram.app;

import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class I18NApp {
 public static void main(String[] args) {
  ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
  System.out.println("First Name: "+ container.getMessage("firstName", null, Locale.ENGLISH));
  System.out.println("Last Name: "+container.getMessage("lastName", null, Locale.ENGLISH));
 }

}

Execute I18NApp.java file. Output is given below.

No comments:

Post a Comment