How do you implement internationalization (i18n) in Spring Boot?

Table of Contents

Introduction

Internationalization (i18n) is the process of designing an application so that it can be easily adapted to different languages and regions. In Spring Boot, internationalization is achieved by supporting multiple languages and locales in the application, allowing users to view content in their preferred language. Spring provides several built-in tools to enable internationalization, including message sources, locale resolvers, and locale change interceptors.

This guide explains how to implement internationalization in Spring Boot applications, including configuring message sources, defining properties files, and handling locale-specific content.

Step 1: Add Dependencies

If you're using Spring Boot 2.x or later, internationalization support is already included in the default dependencies. However, ensure that your pom.xml includes the Spring Boot Starter Web dependency, as it's needed for building web applications.

Maven Dependency

This starter includes everything you need to implement internationalization, such as MessageSource, LocaleResolver, and the necessary view resolver for serving internationalized content.

Step 2: Create Message Property Files

In Spring Boot, messages for internationalization are stored in property files. These files hold key-value pairs for different languages. You create a base message file (usually messages.properties) and then provide language-specific variants, such as messages_en.properties for English, messages_fr.properties for French, etc.

Example: messages.properties (default)

Example: messages_en.properties (English)

Example: messages_fr.properties (French)

Explanation:

  • The messages.properties file is the default message source. When no specific language is chosen, Spring will fall back to this file.
  • messages_en.properties contains English translations, while messages_fr.properties contains French translations.

Step 3: Configure MessageSource Bean

Spring Boot uses the MessageSource bean to load the message properties files. The MessageSource bean reads the appropriate message file based on the current locale.

Example: Configuration in application.properties

You can configure Spring Boot to use the messages.properties files by specifying a MessageSource bean in your application.

Explanation:

  • The ReloadableResourceBundleMessageSource loads the message files from the classpath.
  • The setBasename("classpath:messages") specifies the base name of the message files, which will automatically pick up messages.properties, messages_en.properties, messages_fr.properties, etc.
  • UTF-8 encoding ensures that special characters (e.g., accents in French) are correctly handled.

Step 4: Configure Locale Resolver

The LocaleResolver determines the user's locale, i.e., the language and region used to display messages. By default, Spring Boot uses the Accept-Language header from the HTTP request to determine the locale. However, you can configure it to support other mechanisms, such as query parameters or session-based locale.

Example: Configuring LocaleResolver

Explanation:

  • The CookieLocaleResolver saves the user’s locale preference in a cookie, so it is preserved across requests.
  • You can set the default locale (in this case, English) if no other locale is provided by the user.

Step 5: Add Locale Change Interceptor

If you want to allow users to change their language dynamically (for example, by clicking on a "change language" button), you need to set up a LocaleChangeInterceptor. This interceptor listens for a locale change parameter (typically lang) in the URL and updates the locale accordingly.

Example: Configuring LocaleChangeInterceptor

Explanation:

  • The LocaleChangeInterceptor listens for a lang parameter in the request, and when it is found, it changes the locale accordingly.
  • If the URL contains ?lang=fr, the French language will be applied to the page.

Step 6: Use MessageSource in Views

Once you've set up the MessageSource and LocaleResolver, you can use the localized messages in your views (Thymeleaf, JSP, etc.). For Thymeleaf, the syntax is as follows:

Example: Thymeleaf View (home.html)

Explanation:

  • The th:text="#{welcome.message}" syntax is used to pull the message from the message properties files based on the current locale.
  • Language change links (?lang=en, ?lang=fr) allow the user to switch between English and French.

Step 7: Testing the Application

  1. Start the Application: Run your Spring Boot application using mvn spring-boot:run or through your IDE.
  2. Access the Application: Open http://localhost:8080 in your browser.
  3. Switch Languages: Use the language change links to toggle between English and French. The messages will be automatically updated based on the selected language.

Conclusion

Implementing internationalization (i18n) in a Spring Boot application enables you to provide localized content to users in different regions. By configuring a MessageSource for message properties files, setting up a LocaleResolver, and enabling locale change functionality, you can easily handle multi-language support in your web application.

With the ability to switch languages dynamically, Spring Boot ensures that users can interact with your application in their preferred language, enhancing the overall user experience.

Similar Questions