SpringBoot : Internalization and i18n messages for Rest

Points To Remember

Accept the Locale from Headers e.g

   @RequestHeader("Accept-Language") Locale locale

Step 1 : Define Beans for Internationalization

Add the files in resources/i18n folder as shown in image

Add the following to the Application class ( The main class)

 public LocaleResolver localeResolver() {
  SessionLocaleResolver slr = new SessionLocaleResolver();
  slr.setDefaultLocale(Locale.US); // Set default Locale as US
  return slr;

 public ResourceBundleMessageSource messageSource() {
  ResourceBundleMessageSource source = new ResourceBundleMessageSource();
  source.setBasenames("i18n/messages");  // name of the resource bundle 
  return source;

Now inject the MessageSource Bean where you want to internationalize the message. We will be doing it in the Controller.
Note : You need to define all your resouce bundles in the messageSource bean so that the application can pick them.

    public class HomeController {

       private MessageSource messageSource;

       public String msg(@RequestHeader("Accept-Language") Locale locale){
          return messageSource.getMessage("msg",null,locale);

No comments :

Post a Comment