How do you implement internationalization in a Spring Boot application?
Table of Contents
- Introduction
- Steps to Implement Internationalization in Spring Boot
- Step 1: Configure Message Source
- Step 2: Create Message Properties Files
- Step 3: Define
MessageSourceBean - Step 4: Configure Locale Resolver
- Step 5: Configure
LocaleChangeInterceptor - Step 6: Using Localized Messages in Controllers
- Step 7: Testing Internationalization
- Step 8: Set Default Locale (Optional)
- Conclusion
Introduction
Internationalization (i18n) is the process of designing an application to support multiple languages and regions without engineering changes to the core functionality. In Spring Boot, i18n is commonly achieved by externalizing messages in property files and configuring locale resolution mechanisms to allow for language switching. This guide will explain how to implement internationalization in a Spring Boot application, covering the configuration of message sources, locale resolvers, and usage of localized messages in the application.
Steps to Implement Internationalization in Spring Boot
Step 1: Configure Message Source
Spring Boot provides the MessageSource bean, which is used to manage and load externalized messages from properties files. These files typically contain language-specific messages for different locales.
Example: Configure MessageSource in application.properties
In the application.properties or application.yml, define the MessageSource configuration to point to your messages files.
Here, spring.messages.basename specifies the base name of the message properties files that Spring Boot will use for i18n. By default, Spring Boot will look for files named messages.properties, messages_en.properties, messages_fr.properties, etc., for English and French translations, respectively.
Step 2: Create Message Properties Files
Next, create the message properties files for each supported locale. These files should be placed in the src/main/resources directory.
Example: Create messages.properties (default)
Example: Create messages_fr.properties (French)
Example: Create messages_es.properties (Spanish)
In these files, you define the key-value pairs for your messages, where the key is the same across all files, and the value changes depending on the language.
Step 3: Define MessageSource Bean
You need to define a MessageSource bean in a @Configuration class to provide the localized messages to the Spring context.
Example: Configuring MessageSource Bean
In this configuration:
ReloadableResourceBundleMessageSourceis used to load the message properties files.- The
setBasenamemethod points to the location of the message files, andsetDefaultEncodingensures proper encoding.
Step 4: Configure Locale Resolver
A LocaleResolver is used to determine the current locale for the user. You can configure a SessionLocaleResolver or AcceptHeaderLocaleResolver in Spring Boot.
Example: Configure LocaleResolver Bean
In this example:
- A
SessionLocaleResolveris used, which stores the locale in the user's session. - The default locale is set to
Locale.US(English).
Step 5: Configure LocaleChangeInterceptor
The LocaleChangeInterceptor intercepts requests and allows users to change the locale based on a query parameter, such as ?lang=en or ?lang=fr.
Example: Configure LocaleChangeInterceptor Bean
In this example:
- The
setParamName("lang")method specifies the query parameter (lang) used to change the language. - This interceptor checks the
langparameter in each request and changes the locale accordingly.
Step 6: Using Localized Messages in Controllers
You can now use the localized messages in your Spring Boot controllers or services by injecting the MessageSource and accessing the messages using their keys.
Example: Using MessageSource in a Controller
In this example:
- The
greetmethod accepts alangparameter from the URL (e.g.,/greeting?lang=fr). - Based on the provided language, the correct localized message is returned from the
messages.propertiesfiles.
Step 7: Testing Internationalization
Now, you can test the internationalization feature by accessing the /greeting endpoint with different lang parameters.
/greeting?lang=enreturns "Hello"/greeting?lang=frreturns "Bonjour"/greeting?lang=esreturns "Hola"
Step 8: Set Default Locale (Optional)
If you want to specify a default language when no lang parameter is provided, you can configure the LocaleResolver to fall back to a default locale.
Example: Default Locale in LocaleConfig
Conclusion
Implementing internationalization (i18n) in a Spring Boot application allows your app to cater to users across different languages and regions. By configuring MessageSource, LocaleResolver, and LocaleChangeInterceptor, you can dynamically serve content in multiple languages. This flexibility enhances user experience and ensures that your application is ready for global deployment.