Spring Boot Internationalization

Accept-Language: You can send request with “Accept-Language” parameter of HTTP “Header” as en-US and tr-TR or without parameter. LocaleResolver: Also we change use LocaleResolver to resolve user locale, this interface allows for implementations based on request, session, cookies, etc. [code lang=”java”] @Bean public LocaleResolver localeResolver(){ SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); sessionLocaleResolver.setDefaultLocale(Locale.UK); return sessionLocaleResolver; } [/code] LocaleChangeInterceptor: An other user language resolver is LocaleChangeInterceptor. We can change locale on every request [code lang=”java”] @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("lang"); return localeChangeInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } [/code] For message resource file name convention is basename_languageCode_countryCode.properties. For example you have to create a “messages.properties” file for default and a “message_tr.properties” and “message_en_US.properties”. We must append suffix like as tr_TR (it means Locale(“tr”,”TR”)), en_UK (it means Locale.UK) or en_US (it means Locale.US). We can use dynamic parameter with curly brackets. messages.properties message.not.found = Message Not Found. account.not.found= Account {0} not found message_tr.properties message.not.found = Mesaj bulunamadı. account.not.found= Hesap {0} bulunamadı message_en_UK.properties message.not.found = Message not found. account.not.found= Account {0} not found message_en_US.properties message.not.found = Message not found. account.not.found= Account {0} not found We can call with different parameter count getMessage() method. getMessage(String code, Object[] args, Locale locale) if no corresponding message was found it throws NoSuchMessageException. getMessage(String code, Object[] args, String defaultMessage, Locale locale) defaultMessage runs when default message to return if the lookup fails. Default MessageSource example: Using example: [code lang=”java”] @Autowired MessageSource messageSource; @GetMapping("/") public String sayMessage() { return messageSource.getMessage("message.not.found", null, new Locale("tr","TR")); } [/code] ResourceBundleMessageSource example: You can override MessageSource with new base name syntax in the Configuration class like this. After this configuration you can define these properties files, /src/main/resources/messages/i18n.properties, /src/main/resources/messages/i18n_tr.properties, /src/main/resources/messages/i18n_en_UK.properties, /src/main/resources/messages/i18n_en_US.properties. [code lang=”java”] @Configuration public class Config { @Bean public MessageSource messageSource () { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages/i18n"); return messageSource; } } [/code] You can also use reloadable file with ReloadableResourceBundleMessageSource, it provide change the file content on the fly. [code lang=”java”] @Configuration public class Config { @Bean public MessageSource messageSource () { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); // messageSource.setBasename("classpath:messages/i18n"); messageSource.setDefaultEncoding("UTF-8"); //refresh cache after the one hour messageSource.setCacheMillis(60 * 60 * 1000); return messageSource; } } [/code] Multiple Message Source: And ResourceBundleMessageSource provide us multilpe message resources. We can set multiple resource messages files. When we call the key, it look up from first to end resource messages file and first match will be returned value of key. [code lang=”java”] @Bean public MessageSource messageSource () { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); // messageSource.setBasenames("messages/i18n", "messages/i18n-02"); messageSource.setBasenames("messages", "messages-02"); return messageSource; } [/code] Parent Message Source: And we can set sources in hierarchical order. If key couldn’t find then it look up to parent resources messages file. We can override messages file with parent file we must use setParentMessageSource() like this. [code lang=”java”] @Bean public MessageSource messageSource () { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); // messageSource.setBasename("messages/i18n-02"); messageSource.setBasename("messages-02"); ResourceBundleMessageSource parentMessageSource = new ResourceBundleMessageSource(); // parentMessageSource.setBasename("messages/i18n"); parentMessageSource.setBasename("messages"); messageSource.setParentMessageSource(parentMessageSource); return messageSource; } [/code] For more information you can refer to these posts: howtodoinjava.com spring-boot2/rest/i18n-internationalization zetcode.com spring/messagesource/ logicbig.com tutorials/spring-framework/spring-core/message-sources.html injavawetrust.com spring-15-messagesource javadevjournal.com spring-boot-internationalization/ medium.com/skillhive internalization-in-spring-boot-22f4fa82f132 baeldung.com spring-boot-internationalization baeldung.com spring-custom-validation-message-source]]>

Leave a Reply

Your email address will not be published. Required fields are marked *