Spring Boot Internationalization i18n

 Spring Boot Internationalization i18n:


Rest Api sometimes need to write for different Language readers at that time internationalization is requiered (internationalization has 18 charecters so that by they use i18n)

We will be use of its a HTTP Request Header - Accept Language Header 

  • Accept-Language: indicate natural Language and local that the customer prefers
  • Example: en - English (Good Morning)
  • Example: nl -  Dutch (Goedemorgen)
  • Example: fr - French (Bonjour) etc
HelloWorldController.java

@RestController

public class HelloWorldController {


private MessageSource messageSource;


public HelloWorldController(MessageSource messageSource) {

this.messageSource = messageSource;

}

@GetMapping(path= "/hello-world-i18n")

public String helloWorldi18n() {

//this will provide which language is to pick as its pick from request header

Locale locale = LocaleContextHolder.getLocale();

String message = messageSource.getMessage("good.morning.message", null, "Defalt Message", locale);

return message;

}

}


we can add multiple of diffrent language file with _ using this underscore
we can us language code like fr for france nl for duch like this
messages.properties
good.morning.message=Good Morning
messages_nl.properties
good.morning.message=Goedemorgen
messages_fr.properties
good.morning.message=Bonjour



like this


add the accept-language header in your postman to test




Comments

Popular posts from this blog

Introduction of RESTful Web Service

Learn JPA and Hibernate

Implementing Dynamic Filtering for Rest API