As applications grow and reach global audiences, providing support for multiple languages and cultures becomes essential. Internationalization (i18n) in Spring Boot allows developers to adapt their applications to different languages and regional differences without modifying the core business logic. In this post, we’ll explore how to implement internationalization in a Spring Boot application.
What is Internationalization (I18n)?
Internationalization is the process of designing and developing an application in such a way that it can be easily adapted for different languages and regions (locales) without engineering changes. This includes:
- Translating text displayed in the UI.
- Adapting formats for dates, numbers, and currencies.
- Providing locale-specific content.
Spring Boot Support for Internationalization
Spring Boot provides built-in support for internationalization through message bundles using the ResourceBundleMessageSource
class. This allows for seamless localization of your application.
Setting Up Internationalization in Spring Boot
Let’s go through the steps required to set up internationalization in a Spring Boot application.
1. Create Message Resource Bundles
Create properties files for each supported locale. By default, Spring Boot looks for these resource bundles in the src/main/resources
directory. For example:
messages.properties
(default language)messages_es.properties
(Spanish)messages_fr.properties
(French)
Here’s an example content for these files:
# messages.properties
welcome.message=Welcome to our application!
# messages_es.properties
welcome.message=¡Bienvenido a nuestra aplicación!
# messages_fr.properties
welcome.message=Bienvenue dans notre application !
2. Configuring Message Source
In your Spring Boot application, configure the message source bean to use the created properties files. You can do this in your main application class or a configuration class:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
@Configuration
public class MessageConfig {
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages"); // Name of the properties files without extension
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
3. Using LocaleResolver
Spring provides a LocaleResolver
interface that determines the current locale. You can configure a simple locale resolver to use the session or request for determining the user’s locale:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class LocaleConfig {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
}
4. Creating a Controller
Create a controller that uses the message source to return localized messages:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import java.util.Locale;
@Controller
public class WelcomeController {
@Autowired
private MessageSource messageSource;
@GetMapping("/welcome")
public String welcome(@RequestHeader(name = "Accept-Language", required = false) Locale locale, Model model) {
String welcomeMessage = messageSource.getMessage("welcome.message", null, locale);
model.addAttribute("message", welcomeMessage);
return "welcome";
}
}
5. Creating the View
Finally, create a simple HTML template (assuming you’re using Thymeleaf or a similar template engine) for displaying the welcome message:
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Testing Internationalization
To test the internationalization functionality, you can use a web browser or tools like Postman to send requests with the Accept-Language
header set to the desired locale:
curl -H "Accept-Language: es" http://localhost:8080/welcome
The response should show the welcome message in Spanish:
¡Bienvenido a nuestra aplicación!
Conclusion
Internationalization in Spring Boot is a straightforward process that allows you to serve applications in multiple languages and cater to various locales. By leveraging resource bundles and the provided abstractions, you can easily enhance your applications for a global audience.
For deeper exploration and advanced practices in developing internationalized applications, check out the extensive resources available at ITER Academy, where you can enhance your knowledge of Spring Boot and other related frameworks.