Wednesday, October 8, 2014

Spring MVC Internationalization (i18n)


Spring MVC Internationalization (i18n)

Scope : In this tutorial explained that how to handle the i18n with using the Spring MVC.

Objective : The user input screen language going to change based on the selected language.

Requirements:
  • Spring 3.0 
  • Eclipse JAVA EE IDE
  • JDK 1.6
  • ApacheTomcat - 6.0.26

Step 1: Create a dynamic web project in eclipse and named as "SpringMVC".
             File à New à Dynamic Web Project


Step 2 : Create the DTO class "User" under the "com.prem.dto" package.

package com.prem.dto;

public class User {
 private String username;
 private String password;

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}


Step 3 : Add all the Spring and other relevant jars to WebContent\WEB-INF\lib
              The following jars added for this example. 
    • commons-logging-1.1.jar
    • org.springframework.asm-3.0.1.jar
    • org.springframework.beans-3.0.1.jar
    • org.springframework.context-3.0.1.jar
    • org.springframework.core-3.0.1.jar
    • org.springframework.expression-3.0.1.jar
    • org.springframework.web.servlet-3.0.1.jar
    • org.springframework.web-3.0.1.jar
    • jstl.jar

Step 4 : Create the controller class "LoginController" under the "com.prem.spring.controller"               package.


package com.prem.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.prem.dto.User;

@Controller
@SessionAttributes
public class LoginController {

 @RequestMapping(value="/login", method = RequestMethod.POST)
 public String authenticate(@ModelAttribute("loguser") User user){
  return "redirect:showLogin.html";
 }
 
 @RequestMapping("/showLogin")
 public ModelAndView showLogin(){
  return new ModelAndView("login", "command", new User());
 }
}


Step 5 : For this example, there are two properties file created and the files are stored as 
              UTF-8 format.

The following properties defined in messages_en.properties file.
label.username=First Name
label.password=Last Name
label.login=Login
label.title=Language

label.footer=© http://thepathpasses.blogspot.com

The following properties defined in messages_ta.properties file. 
label.username=பயனர் பெயர்
label.password=கடவுச்சொல்
label.login=உள்நுழைவு
label.title=பயன்படு மொழி

label.footer=© http://thepathpasses.blogspot.com

Step 6 : Now the time for do the configurations. The web.xml file looks like below. 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>SpringMVCi18n</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>

 <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>

</web-app>
The standard spring configuration defined in the <servlet> and <servlet-mapping> tags.

For the i18n purpose the spring encodingFilter defined in <filter> tag with the initialization parameters.

Step 7 : The "spring-servlet.xml" file looks as below.


<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"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">


 <context:component-scan
  base-package="com.prem.spring.controller" />

 <bean id="viewResolver"
         class="org.springframework.web.servlet.view.UrlBasedViewResolver">
         <property name="viewClass"
             value="org.springframework.web.servlet.view.JstlView" />
         <property name="prefix" value="/WEB-INF/jsp/" />
         <property name="suffix" value=".jsp" />
  </bean>


 <!-- Application Message Bundle -->
 <bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basename" value="classpath:messages" />
  <property name="defaultEncoding" value="UTF-8"/>
        <property name="useCodeAsDefaultMessage" value="false"/>
 </bean>

 <bean id="localeChangeInterceptor"
  class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  <property name="paramName" value="lang" />
 </bean>

 <bean id="localeResolver"
  class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

 <bean id="handlerMapping"
  class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <property name="interceptors">
   <ref bean="localeChangeInterceptor" />
  </property>
 </bean>


</beans>


Step 7 : Now the time to create the view pages. For this example "header.jsp" and "login.jsp" files are created.

The "header.jsp" files looks as below.

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<h3><spring:message code="label.title"/></h3>

<span style="float: left">
 <a href="?lang=en">English</a> 
 | 
 <a href="?lang=ta">Tamil</a>
</span>

Above there are 2 links provide to user to select the language.


And the "login.jsp" files looks as below.

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page contentType="text/html;charset=UTF-8" %>

<!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=UTF-8'>
<title>Login form</title>
</head>
<body>

<form:form method="post" action="login.html">
 <%@ include file="header.jsp" %>
 <br />
 <table>
 <tr>
  <td><form:label path="username"><spring:message code="label.username"/></form:label></td>
  <td><form:input path="username" /></td> 
 </tr>
 <tr>
  <td><form:label path="password"><spring:message code="label.password"/></form:label></td>
  <td><form:password path="password" /></td>
 </tr>
 <tr>
  <td colspan="2">
   <input type="submit" value="<spring:message code="label.login"/>"/>
  </td>
 </tr>
</table> 
</form:form>

</body>
</html>


Step 8 : Now the time to execute the program and see the results. Right click on the "SpringMVCi18n" project and go to Run As - Run on Server.

The initial screen looks as below.



Once change the language as Tamil, the screen look like as below.



That's all dears...


E.E (Error Experienced): 

1. Import the ModelAndView from org.springframework.web.servlet.ModelAndView 
    NOT org.springframework.web.portlet.ModelAndView 

2. Use the proper RequestMethod (POST or GET) in the @RequestMapping.

3. Use the proper action method (POST or GET) in  the login.jsp file

4. Define the correct content type as "<%@ page contentType="text/html;charset=UTF-8" %>" NOT "<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>"

5. Use the "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>" header meta tag instead of "<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">"

1 comment:

  1. To easily internationalize your application, my suggestion is to try a software collaborative translation management platform like https://poeditor.com/. It has a simple and friendly interface and can simplify the workflow.

    ReplyDelete