Spring MVC Internationalization (i18n)

Internationalization(i18n) is useful in web applications when we wanted our application to support multiple
languages. Spring MVC provide support to internationalization using ReloadableResourceBundleMessageSource,
CookieLocaleResolver and LocaleChangeInterceptor.

In this tutorial, we are going to create a  maven web application to show the internationalization feature.

1-Create a maven web application.
2-Create a folder structure as shown in below screenshot.



3- Java Files


EmployeeController.java

package com.skuera.controller;


import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;


import com.skuera.vo.EmployeeVo;


/**

 * @author skuera
 *
 */
@Controller
public class EmployeeController{


@RequestMapping(value = "insertEmployee.view")
public String toInsertEmployee(Model model) {
model.addAttribute("employeeForm", new EmployeeVo());
return "insertEmployee";
}




}

EmployeeVo.java


package com.skuera.vo;

/**

 * @author skuera
 *
 */
public class EmployeeVo {

private int empId;
private String empName;

public EmployeeVo(){}

public EmployeeVo(int empId, String empName) {

super();
this.empId = empId;
this.empName = empName;
}

public int getEmpId() {

return empId;
}

public void setEmpId(int empId) {

this.empId = empId;
}

public String getEmpName() {

return empName;
}

public void setEmpName(String empName) {

this.empName = empName;
}



}


Properties file



messages_en.properties  - English

employee.head=Insert Employee
employee.empId=Enter Employee ID
employee.empName=Enter Employee Name
employee.submit=Insert Employee

messages_fr.properties  - French

employee.head=Insérer des employés
employee.empId=Entrez Employee ID
employee.empName=Entrez Nom de l'employé
employee.submit=Insérer des employés


messages_in.properties  - Hindi

employee.head=\u0928\u090F \u0915\u0930\u094D\u092E\u091A\u093E\u0930\u0940 \u0921\u093E\u0932\u0947\u0902
employee.empId=\u0915\u0930\u094D\u092E\u091A\u093E\u0930\u0940 \u092A\u0939\u091A\u093E\u0928
employee.empName=\u0915\u0930\u094D\u092E\u091A\u093E\u0930\u0940 \u0915\u093E \u0928\u093E\u092E
employee.submit=\u091C\u092E\u093E \u0915\u0930\u0947\u0902

messages_in.properties  - Vietnamese

employee.head=Chèn nhân viên
employee.empId=Nh\u1EADp ID nhân
employee.empName=Tên nhân viên
employee.submit=Chèn nhân viên


Spring Configuration files


web.xml

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FormValidate</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

<servlet>

<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>

<servlet-name>SpringMvc</servlet-name>
<url-pattern>*.view</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> 

</web-app>

SpringMvc-servlet.xml

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="com.skuera.*"/>
<context:annotation-config />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="com.skuera.*" />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="in" />
</bean>

<bean id="localeChangeInterceptor"

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

</beans>



JSP Pages


insertEmployee.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>Insert Employee</title>
</head>
<body>
<div align="center">
<h1><spring:message code="employee.head"/></h1>
</div>
<div align="center">
<form:form action="insertNewEmployee.view" method="GET"
commandName="employeeForm">
<table border="2px" cellpadding="2" style="color: gray; background-color: aqua;">
<tr>
<td><spring:message code="employee.empId"/> :</td>
<td><form:input path="empId" /></td>
</tr>
<tr>
<td><spring:message code="employee.empName"/> :</td>
<td><form:input path="empName" /></td>
</tr>
<tr>
<td colspan="2" ><input type="submit" value='<spring:message code="employee.submit"/>' /></td>
</tr>
</table>
</form:form>
</div>
<hr>
<div align="center">
Select language : <a href="?language=en">English</a> | <a href="?language=fr">French</a> | <a href="?language=in">Hindi</a> | <a href="?language=vt">Vietnamese</a>
</div>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<a href="insertEmployee.view">Go to form</a>
</body>
</html>

English
Internationalization in Spring_JavaInstance

Hindi
Internationalization in Spring JavaInstance
Vietnamese




Hope you will like the tutorial ...Happy Learning

You may be interested in Send Mail in Spring MVC using Gmail SMTP.

Give you suggestion in comments.If you like the post Share with your friends on social network.
Spring MVC Internationalization (i18n) Spring MVC Internationalization (i18n) Reviewed by JavaInstance on 11:14:00 AM Rating: 5

No comments:

Powered by Blogger.