Spring MVC Internationalization Example

Internationalization (I18N) represents a process of designing an application for the global market. In order words, it is a process of designing an application that can be adapted to various languages and regions. In this tutorial, we will show how to implement internationalization using the Spring MVC framework.

1. Introduction

1.1 Spring Framework

  • Spring is an open-source framework created to address the complexity of an enterprise application development
  • One of the chief advantages of the Spring framework is its layered architecture, which allows a developer to be selective about which of its components they can use while providing a cohesive framework for J2EE application development
  • Spring framework provides support and integration to various technologies for e.g.:
    • Support for Transaction Management
    • Support for interaction with the different databases
    • Integration with the Object Relationship frameworks for e.g. Hibernate, iBatis etc
    • Support for Dependency Injection which means all the required dependencies will be resolved with the help of containers
    • Support for REST style web-services

1.2 Spring Mvc Framework

Model-View-Controller (Mvc) is a well-known design pattern for designing the GUI based applications. It mainly decouples the business logic from UI by separating the roles of Model, View, and Controller in an application. This pattern divides the application into three components to separate the internal representation of the information from the way it is being presented to the user. The three components are:

  • Model (M): Model’s responsibility is to manage the application’s data, business logic, and the business rules. It is a POJO class which encapsulates the application data given by the controller
  • View (V): A view is an output representation of the information, such as displaying information or reports to the user either as a text-form or as charts. Views are usually the JSP templates written with Java Standard Tag Library (JSTL)
  • Controller (C): Controller’s responsibility is to invoke the Models to perform the business logic and then update the view based on the model’s output. In the spring framework, the controller part is played by the Dispatcher Servlet

Spring MVC Internationalization - Model View Controller (MVC) Overview
Fig. 1: Model View Controller (MVC) Overview

Now, open up the Eclipse IDE and let’s see how to achieve the internationalization for an application in the spring mvc framework!

2. Spring MVC Internationalization Example

Here is a step-by-step guide for implementing this functionality in the spring mvc framework.

2.1 Tools Used

We are using Eclipse Kepler SR2, JDK 8 and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.

2.2 Project Structure

Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!

Spring MVC Internationalization - Application Project Structure
Fig. 2: Application Project Structure

2.3 Project Creation

This section will demonstrate how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project.

Spring MVC Internationalization - Create a Maven Project
Fig. 3: Create a Maven Project

In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location’ will be selected. Just click on next button to proceed.

Spring MVC Internationalization - Project Details
Fig. 4: Project Details

Select the Maven Web App archetype from the list of options and click next.

Spring MVC Internationalization - Archetype Selection
Fig. 5: Archetype Selection

It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT.

Spring MVC Internationalization - Archetype Parameters
Fig. 6: Archetype Parameters

Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:


<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

We can start adding the dependencies that developers want like Servlet API, Spring Mvc etc. Let’s start building the application!

3. Application Building

Below are the steps involved in developing this application.

3.1 Maven Dependencies

Here, we specify the dependencies for the spring mvc framework. The rest dependencies such as Spring Beans, Spring Core etc. will be automatically resolved by Maven. The updated file will have the following code:


<project xmlns="" xmlns:xsi=""
	<name>SpringMvcInternationalization Maven Webapp</name>
		<!-- Servlet API Dependency -->
		<!-- Spring Framework Dependencies -->
		<!-- -->

3.2 Configuration Files

Let’s write all the configuration files involved in this application.

3.2.1 Web Deployment Descriptor

The web.xml file declares one servlet (i.e. Dispatcher Servlet) to receive all kind of the requests. Dispatcher servlet here acts as a front controller. Add the following code to it:


 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >




3.2.2 Spring Configuration File

To configure the spring framework, developers need to implement a bean configuration file i.e. springmvcinternationaldispatcher-servlet.xml which provide an interface between the basic Java class and the outside world. Add the following code to it:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:context=""

	<!-- To activate the stereotype annotations -->
	<context:component-scan base-package="com.spring.intl.controller" />

	<context:annotation-config />

	<!-- For reading the properties files -->
	<bean id="messageSource" class="">
		<property name="basename" value="/WEB-INF/message" />

	<!-- Locale Change Interceptor to achieve internationalization. -->
			<mvc:mapping path="/init" />
			<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
				<property name="paramName" value="lang"></property>

	<!-- Cookie Resolver -->
	<bean id="localeResolver"

	<!-- For resolving the view name and invoking the particular view page for 
		the user -->
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp"></property>

Do note:

  • This file is loaded by the spring’s Dispatcher Servlet which receives all the requests coming into the application and dispatches them to the controller for processing
  • This file has the ReloadableResourceBundleMessageSource bean declaration that tells the framework to enable the Internationalization (i18N) in the application. Here,
    • The basename property is a mandatory attribute that provides the location of the resource bundles
  • This file has the LocaleChangeInterceptor bean declaration that tells that framework to intercept the incoming HTTP requests and checks for any special parameter in the requests. The parameter name that has to be searched in the incoming HTTP requests is specified by the paramName property
  • This file has the CookieLocaleResolver bean declaration that tells the framework to resolve the locale based on the pre-defined attribute in the cookie
  • This file has the InternalResourceViewResolver bean declaration that tells the framework how to find the physical JSP files according to the logical view names returned by the controllers, by attaching the prefix and the suffix to a view name

3.3 Message Resource Files

We will create two properties files which will contain all the messages to be displayed in the application. Create two files i.e. and in the SpringMvcInternationalization/src/main/webapp/WEB-INF folder and add the following code to it:

welcome.message = Hello, Daniel. Good to meet you.

welcome.message = Salut Daniel. Heureux de vous rencontrer.

3.4 Java Class Creation

Let’s create a simple class where the @Controller annotation specifies this class as a spring controller and is responsible for handling the incoming requests. Add the following code to it:

package com.spring.intl.controller;

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

public class WelcomeCtrl {

	@RequestMapping(value= "/init", method= RequestMethod.GET)
	public ModelAndView initView() {
		System.out.println("Handler method is called.");

		ModelAndView modelview = new ModelAndView();
		modelview.addObject("welcome_msg", "Spring Mvc Internationalization Example");		
		return modelview;

3.5 JSP View

Spring Mvc supports many types of views for different presentation technologies. These include – JSP, HTML, XML etc. So let us write a simple result view in SpringMvcInternationalization/src/main/webapp/WEB-INF/views folder.

3.5.1 Output Page

This is the output page of the example demonstrating the Internationalization in spring mvc framework. The default locale of the application will be English; however, there are two links in this page for either selecting the en or the fr locale. Add the following code to it:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="spring" uri="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<hr />		
		<a id="en" href="/SpringMvcInternationalization/init?lang=en">English</a> | <a id="fr" href="/SpringMvcInternationalization/init?lang=fr">French</a>
		<div> </div>
		<div id="welcome_text">			
			<h4><spring:message code="welcome.message" /></h4>

4. Run the Application

As we are ready with all the changes, let us compile the project and deploy the application on the Tomcat7 server. To deploy the application on Tomat7, right-click on the project and navigate to Run as -> Run on Server.

Spring MVC Internationalization - Deploy Application on Tomcat
Fig. 7: How to Deploy Application on Tomcat

Tomcat will deploy the application in its web-apps folder and shall start its execution to deploy the project so that we can go ahead and test it on the browser.

5. Project Demo

Open your favorite browser and hit the following URL to display the application’s output page.


Server name (localhost) and port (8082) may vary as per your tomcat configuration.

Spring MVC Internationalization - Welcome page in English (i.e. Locale: en)
Fig. 8: Welcome page in English (i.e. Locale: en)

By default en locale is selected. Users can click on the given language links to change the application text as per their preference.

Spring MVC Internationalization - Welcome page in French (i.e. Locale: fr)
Fig. 9: Welcome page in French (i.e. Locale: fr)

That’s all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and don’t forget to share!

6. Conclusion

In this section, developers learned how to implement internationalization in the spring mvc framework. Developers can download the sample application as an Eclipse project in the Downloads section.

7. Download the Eclipse Project

This was an example of Spring MVC Internationalization.

You can download the full source code of this example here: SpringMvcInternationalization


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button