Skip to main content

Command Palette

Search for a command to run...

Spring MVC Internationalization Example

Updated
Spring MVC Internationalization Example
Y

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.

Date: 2018-08-23

Internationalization in Spring MVC: A Comprehensive Guide

Internationalization (I18n), the process of adapting software to various languages and regions, is crucial for reaching a global audience. This article delves into how to implement I18n within the Spring MVC framework, a popular Model-View-Controller (MVC) design pattern used for building web applications. MVC separates an application's concerns into three interconnected parts: the Model, representing data and business logic; the View, responsible for presenting data to the user; and the Controller, which manages user interaction and updates the Model accordingly. This separation simplifies development, improves maintainability, and allows for easier adaptation to different needs, including I18n.

The process of internationalizing a Spring MVC application involves several key steps. First, we need a structured project setup. Typically, this would involve using a build tool like Maven to manage dependencies and project structure. Maven streamlines the process of including necessary libraries, such as the Spring MVC framework itself, and its associated components like Spring Beans and Spring Core. These components provide the foundational elements for building the application. Within this structure, certain files play critical roles.

A central configuration file, often named web.xml (though the exact name might vary based on project setup), acts as the application's main dispatcher. It's like a traffic controller, directing incoming requests to the appropriate parts of the application. In this context, it sets up the Spring dispatcher servlet, a core component of Spring MVC, to handle all incoming requests.

Next, a Spring configuration file, such as springmvcinternationaldispatcher-servlet.xml, configures the Spring framework's behavior. This file defines beans—objects that encapsulate application components—and their dependencies. It establishes the connections between different parts of the application and sets up how the Spring framework manages them. This includes configuring the I18n aspects, specifying how the application should handle different locales and load corresponding resources.

The heart of I18n lies in resource bundles. These bundles are essentially collections of key-value pairs, where keys represent messages or text within the application, and values are their translations in different languages. In our example, files named message_en.properties and message_fr.properties would contain English and French translations respectively. Each file stores the same set of keys but maps them to different translated values. This approach allows the application to easily switch between languages based on user preference or system settings.

Within the application's code, controllers handle user input and interaction. For instance, a controller annotated with @Controller in a class like WelcomeCtrl would process requests, retrieving data from the model and choosing the appropriate view to display. The controller's role is pivotal in facilitating the I18n process by using the Spring framework's mechanism to fetch localized messages from the resource bundles based on the current locale.

Views, usually JSPs (JavaServer Pages) or other template engines, display information to the user. In the example, a JSP file named welcome.jsp would render the output. Importantly, the JSP would not contain hard-coded text but rather utilize Spring's expression language to dynamically display messages from the selected resource bundle, ensuring that the displayed text matches the user's chosen language. For example, instead of hardcoding "Welcome", the JSP would display a message associated with a key like "welcome.message" which would then pull the corresponding translation from the relevant properties file based on the locale.

The entire process works in tandem. When a user accesses the application, the framework determines their preferred locale (e.g., based on browser settings). This locale is then used to select the correct resource bundle. The controller processes the request, retrieves the appropriate localized messages from the selected bundle, and passes them to the view. Finally, the view renders the content using the localized messages, providing a tailored user experience.

The user interface itself would typically include mechanisms for selecting language preferences. This could be as simple as links allowing users to switch between languages, as depicted in the figures showing English and French versions of the welcome page. Clicking these links would trigger a change in the locale setting and subsequently refresh the page with the corresponding translated content.

Deployment usually involves packaging the application (typically a WAR file) and deploying it to a web server such as Tomcat. After deployment, users can access the application via a web browser. The application will automatically handle the I18n based on the user's locale settings, providing a seamless multilingual experience.

In summary, internationalizing a Spring MVC application involves structuring your project correctly, defining resource bundles containing localized messages, configuring Spring to manage these bundles and handle locale changes, and adapting your controllers and views to utilize localized content dynamically. The separation of concerns inherent in the MVC pattern facilitates this process, making it easier to manage different languages and regions, ultimately allowing your application to reach a significantly wider global user base. Properly structuring your resources and leveraging Spring MVC's built-in support makes the process streamlined and efficient, leading to a more inclusive and user-friendly application.

Read more

More from this blog

The Engineering Orbit

1174 posts

The Engineering Orbit shares expert insights, tutorials, and articles on the latest in engineering and tech to empower professionals and enthusiasts in their journey towards innovation.

Spring MVC Internationalization Example