Signup/Sign In

Spring I18N (Internationalization)

I18N is an acronym for internationalization, Spring framework uses it to make an application for the international level. It means we can create an application for multi region-specific like an application can be used in US, China, Russia, and in any other country of the world just by implementing i18N.

The Spring framework uses LocaleResolver to support the internationalization and localization as well. In this tutorial, we will learn to implement internationalization in our spring MVC application.

Let's create a maven based spring project, our project looks like the below and contains several source code files.

Project Structure


This is a configuration file which is an alternate of the applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @ComponentScan annotation is used to indicate the location of the component class in our spring project.

Here, we configured our application for i18n support. You can notice that apart from ViewResolver, we added a couple of methods to get a lang parameter.

package com.studytonight;

import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class AppConfig implements WebMvcConfigurer{

	@Bean(name = "messageSource")
    public ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        return messageSource;
	public ViewResolver viewResolver() {
		InternalResourceViewResolver irvr = new InternalResourceViewResolver();
		return irvr;

	public LocaleResolver localeResolver() {
		SessionLocaleResolver slr = new SessionLocaleResolver();
		return slr;

	public LocaleChangeInterceptor localeChangeInterceptor() {
		LocaleChangeInterceptor localeChangeInterceptor =  new LocaleChangeInterceptor();
		return localeChangeInterceptor;

	public void addInterceptors(InterceptorRegistry registry) {


This file contains code to create an IOC container for our application. The AnnotationConfigWebApplicationContext class is used to create an object for application context.

package com.studytonight;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer {

	public void onStartup(ServletContext servletContext) throws ServletException { 
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));


This is our controller class that returns an index JSP page to the browser when the application starts.

package com.studytonight.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

public class HomeController {
	public String index() {
		return "index";

These are our property files, the first contains values for the English language and the second contains Spanish language values.



label.register=Register Here


#labels electrónico

label.register = Registrarse aquí

// index.jsp

This JSP file uses the <spring:message> tag to get the values from the property files and display them accordingly.

<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ taglib prefix="form" uri=""%>
<%@ taglib prefix="spring" uri=""%>
<!DOCTYPE html>
<meta charset="UTF-8">
<title>User Form</title>
	<form action="register" method="post">
		<spring:message code=""/>
		<input type="text" name="name"/>
		<spring:message code=""/>
		<input type="email" name="email" />
		<input type="submit" value="<spring:message code="label.register"/>">

// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

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

Run the Application

After successfully completing the project and adding the dependencies run the application using the server and you will get the output as below.

The above form page is displayed in the US English language(default). But if we want to display it in any other language then we can do this just by setting a lang parameter to the URL. See the below output, where we passed lang=es to display the web page in the Spanish language.

About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.