Can you provide an example on how I can add internationalization to my Spring Boot app?

Hi, everyone! Could you share an example of how I can incorporate internationalization into my Spring Boot app?

Hi, Tasha! Sure, here’s a step-by-step example. Start by creating a project using Spring Initializer. It’s super handy and makes project creation a lot simpler. Once you’ve downloaded and unzipped the project, open it up in your preferred IDEA. I personally recommend giving IntelliJ IDEA a try.

Create a CustomLocaleResolver class that will handle the user’s locale. It’s simple, just paste the following code:

public class CustomLocaleResolver 
             extends AcceptHeaderLocaleResolver 
             implements WebMvcConfigurer {

   List<Locale> LOCALES = Arrays.asList(
         new Locale("en"),
         new Locale("fr"));

   public Locale resolveLocale(HttpServletRequest request) {
      String headerLang = request.getHeader("Accept-Language");
      return headerLang == null || headerLang.isEmpty()
            ? Locale.getDefault()
            : Locale.lookup(Locale.LanguageRange.parse(headerLang), LOCALES);

   public ResourceBundleMessageSource messageSource() {
      ResourceBundleMessageSource rs = new ResourceBundleMessageSource();
      return rs;

Look at this to follow the remaining steps.

1 Like

Great! It’s very useful. Thanks a lot!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.