Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Spring Message and Internationalization

In this part we will discuss about Internationalization (I18N) and Localization (L10N) in Spring 3.0. Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. The term is frequently abbreviated as i18n (where 18 stands for the number of letters between the first i and last n in internationalization). Above definition essentially means making changes in your application such that it should be usable in multiple locales (or languages in simple words). Here we will use ResourceBundleMessageSource class for achive internationalization in spring. See the following example:

 

Example of Spring Internationalization

Employee.java

package internationalization;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Employee {
private int id;
private String name;

@PostConstruct
public void initializeEmployee() {
System.out.println("Init of Employee");
}

@PreDestroy
public void destroyEmployee() {
System.out.println("Destroy of Employee");
}

public int getId() {
return id;
}

public void setId(int id) {
this .id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

}

context.xml

........
........
<bean id="emp" class="internationalization.Employee">
<property name="id" value="1" />
<property name="name" value="Test" />
</bean>

<bean id="messageSource" class=
"org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>message</value>
</list>
</property>
</bean>
<context:annotation-config/>
</beans>

message.properties
name=Test
 
 

Main.java

package internationalization;

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

public class Main {

public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext
("internationalization/context.xml");
System.out.println(context.getMessage("name", null,
"Default Message", null));
context.registerShutdownHook();
}
}

In above class context.getMessage(..) have four parameters. First parameter is the name of property which is search in property file. Second parameter here is the dynamically values like place holder. The third parameter is the default value if key value is not found in property file. Fourth parameter is the locale value.

Now we will discuss resource variable in java class file. Let see how to access resource variable and place value in place holder

Example of Spring Internationalization

Employee.java

package internationalization;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

public class Employee {
private int id;
private String name;
@Autowired
private MessageSource messageSource;

public MessageSource getMessageSource() {
return messageSource;
}

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

public void getEmpDetails() {
System.out.println(this.messageSource.getMessage("user.detail.message",
new Object[] { getId(), getName() }, "Default User Message", null));
}

@PostConstruct
public void initializeEmployee() {
System.out.println("Init of Employee");
}

@PreDestroy
public void destroyEmployee() {
System.out.println("Destroy of Employee");
}

public int getId() {
return id;
}

public void setId(int id) {
this .id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

}

Here we have use MessageSource interface that is implemented by various implementation and it is available in org.springframework.context. Now I have injected messageproperty by autowire annotation and place value through place holder

message.properties
id=1
name=Test
user.detail.message=User id is {0} and Name is {1}
 
 

Main.java

package internationalization;

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

public class Main {

public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext
("internationalization/context.xml");
System.out.println(context.getMessage("name", null,
"Default Message", null));
e.getEmpDetails();
context.registerShutdownHook();
}
}


     << Previous
Next >>