Pages

Wednesday, August 4, 2010

Spring - Internationalization i18N


In Spring MVC application, comes with few “LocaleResolver” to support the internationalization or multiple languages features. In this tutorial, it shows a simple user form page, display the message from properties file, and change the locale based on the selected language link.

Web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>disp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>disp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/disp-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

redirect.jsp
We have declared welcome-file-list in web.xml as redirect.jsp, So the first request comes to redirect.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("userRegistration.htm"); %>

disp-servlet.xml
In the web.xml file we have declared 
<?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.xsd">
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages" />
<bean id="userValidator" class="com.UserValidator"/>
<bean name="/userRegistration.htm" class="com.UserController"
p:formView="userForm" p:successView="userSuccess"
p:validator-ref="userValidator" />
</beans>
1.SessionLocaleResolver:
Register a “SessionLocaleResolver” bean, named it exactly the same characters “localeResolver“. It resolves the locales by getting the predefined attribute from user’s session.
Note *** If you do not register any “localeResolver”, the default AcceptHeaderLocaleResolver will be used, which resolves the locale by checking the accept-language header in the HTTP request.
2.LocaleChangeInterceptor
Register a “LocaleChangeInterceptor” interceptor and reference it to any handler mapping that need to supports the multiple languages. The “paramName” is the parameter value that’s used to set the locale.
In this case,
userRegistration.htm?language=en – Get the message from English properties file(messages.properties)
userRegistration.htm?language=fr_FR – Get the message from French properties file(messages_fr.properties)
In the ResourceBundleMessageSource, we have given the basename as messages. So the properties file name should be messages.properties

messages.properties
username=User Name
password=Password
gender=Gender
male=Male
female=Female
aboutYou=About You
name.required=User name is required
password.required=Password is required
gender.required=Gender is required
aboutYou.required=About you is required
messages_fr.properties
username=Nom d'utilisateur
password=Mot de passe
gender=Sexe
aboutYou=À propos de vous
male=mâle
female=féminin
name.required=Nom d'utilisateur est nécessaire
password.required=Mot de passe est requis
gender.required=Sexe est requis
aboutYou.required=À propos de vous est requis

As we discussed privious example (Simple Form Validation), I am not going in depth of the Validations and Controllers.
User.java
package com;

public class User {

private String name;
private String password;
private String gender;
private String aboutYou;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAboutYou() {
return aboutYou;
}
public void setAboutYou(String aboutYou) {
this.aboutYou = aboutYou;
}
}

UserController.java
package com;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class UserController extends SimpleFormController {
public UserController() {
setCommandClass(User.class);
setCommandName("user");
}

protected ModelAndView onSubmit(Object command) throws Exception {
User user = (User) command;
return new ModelAndView("userSuccess","user",user);
}
}

UserValidator.java
package com;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class UserValidator implements Validator {

public boolean supports(Class arg0) {
return User.class.isAssignableFrom(arg0);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
ValidationUtils.rejectIfEmpty(errors, "gender", "gender.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aboutYou", "aboutYou.required");
User user = (User) target;
}
}

UserForm.jsp
A JSP page, contains two hyperlinks to change the locale manually, and use the spring:message to display the message from the corresponds properties file by checking the current user’s locale.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<style>
.error{
font-style:italic;
color:#ff0000;
}
</style>
</head>
<body>
<form:form method="POST" commandName="user">
<form:errors path="*" cssClass="error"></form:errors><br>
Select your Language : <a href="?language=en">English</a>|<a href="?language=fr_FR">French</a>
<table>
<tr>
<td><spring:message code="username" /></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><spring:message code="password" /> :</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><spring:message code="gender" /> :</td>
<td><spring:message code="male" /> : <form:radiobutton path="gender" value="M"  /> 
<spring:message code="female" /> : <form:radiobutton path="gender" value="F"  /></td>
</tr>
<tr>
<td><spring:message code="aboutYou" /> :</td>
<td><form:textarea path="aboutYou" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>

default Language is English

If user is not enter any data

If user selects the FRENCH in the userForm





If user is not entered any date

No comments:

Post a Comment