Spring Boot Internationalization

In this tutorial, we will be discussing about the feature of internationalization which is very useful for international applications. We keep visiting many web applications with multiple languages like russian, french, spanish etc. Let us understand this feature in spring boot.

Java has a concept of Locale which is captured from the request sent by the client. We will be displaying the message according to the locale received from the rest client. We need to create some beans to implement this.

Required Beans for Internationalization:

  • We need to create a Configuration class to create the beans required which are ResourceBundleMessageSource and AcceptHeaderLocaleResolver.

  • Create a class under the root package, we have named the class as BeansConfiguration and the code is as follows:

    @Configuration
    public class BeansConfiguration {
    
         AcceptHeaderLocaleResolver resolver;
         ResourceBundleMessageSource messageSource;
    
         @Bean
         public AcceptHeaderLocaleResolver localeResolver() {
             resolver = new AcceptHeaderLocaleResolver();
             resolver.setDefaultLocale(Locale.US);
             return resolver;
         }
        
         @Bean
         public ResourceBundleMessageSource messageSource() {
             messageSource = new ResourceBundleMessageSource();
             messageSource.setBasename("welcome");
             return messageSource;
         }
      }

  • The above created beans are loaded into SpringApplicationContext during application launch and let us develop a sample REST API serving the request.
  • Add the below rest end point in the ResourceController.java which we had created earlier.

  • We need some properties file where the message will be picked on the basis of locale sent by the client.

  • Create the files welcome.properties, welcome_rs.properties, welcome_fr.properties under src/main/properties and save the below properties in each respectively:

    • welcome.to.country="Welcome to USA"

    • welcome.to.country="Dobro pozhalovat' v Rossiyu"

    • welcome.to.country="Bienvenue en France"

  • Observe that, the basename of message source and file must match(welcome) in the example. (refer to the bean code of ResourceBundleMessageSource.)

    public class ResourceController {
    
        @Autowired
        ResourceBundleMessageSource messageSource;   //Autowiring
    
        @GetMapping("/getMessage")
        public String getLocaleMessage(@RequestHeader(name="Accept-Language", required=false) Locale locale) {
            return messageSource.getMessage("welcome.to.country",null,locale);
    }
        //Other end-points
    }

  • Observe that the above code captures the request header sent by the client and binds it with the

Locale instance and returns the message according to the property "welcome.to.country".

  • We need to autowire the ResourceBundleMessageSource bean which was created above in the Configuration class to use the methods it provide.

  • Let us test the API from the Rest client.

  • The above figure 10.1 displays the response when no request header was sent and returns the text as the default locale selected was Locale.US and the message was displayed from the file welcome.properties.

  • The above figure 10.2 displays the response when the Request header of Accept-Language is set to rs matching the welcome_rs.properties file and thus displaying the welcome message in russian. Similarly figure 10.3 displays the message in french.

Auto-Configuration of ResourceBundleMessageSource:

  • We have created two beans for AcceptHeaderLocaleResolver and ResourceBundleMessageSource.

  • We can also use the application.properties file to allow spring to create the bean of ResourceBundleMessageSource using the below property.

spring.messages.basename=welcome

Conclusion: In this tutorial, we have learnt the usage of internationalization using spring-boot and developed an API to get the message for the request sent by the client. As a quick exercise, comment the
Bean code of ResourceBundleMessageSource (In BeansConfiguration.java), add the above property and test the API from Rest-client.