Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validation messages in messages.properties file for i18n internationalization by locale in spring boot
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RequestMapping("/")
@ResponseBody
String home() {
return "
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:locale/messages");
messageSource.setCacheSeconds(3600); //refresh cache once per hour
return messageSource;
}
}
public interface MessageByLocaleService {
public String getMessage(String id);
}
@Component
public class MessageByLocaleServiceImpl implements MessageByLocaleService {
@Autowired
private MessageSource messageSource;
@Override
public String getMessage(String id) {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(id,null,locale);
}
}
user.login.invalid.id=0
user.login.invalid=Your user name or password was invalid
@RestController
@RequestMapping("/user")
public class UserControler {
@Autowired
UserService userService;
@Autowired
MessageByLocaleService messageByLocaleService;
@RequestMapping(value = "/userlogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> login(
@RequestBody UserCred userCred)
throws UnsupportedEncodingException {
String invalidLogin = messageByLocaleService.getMessage("user.login.invalid");
}
}
@jmartineztalosdigital

This comment has been minimized.

Show comment
Hide comment
@jmartineztalosdigital

jmartineztalosdigital Sep 22, 2015

great!!! thanks.

My only change @Autowired

@component
public class MessageByLocaleServiceImpl implements MessageByLocaleService {

@Autowired
private MessageSource messageSource;

jmartineztalosdigital commented Sep 22, 2015

great!!! thanks.

My only change @Autowired

@component
public class MessageByLocaleServiceImpl implements MessageByLocaleService {

@Autowired
private MessageSource messageSource;
@fallphenix

This comment has been minimized.

Show comment
Hide comment
@fallphenix

fallphenix Jun 3, 2017

Great!!!! thanks a lot...
so i used @Autowired

fallphenix commented Jun 3, 2017

Great!!!! thanks a lot...
so i used @Autowired

@anataliocs

This comment has been minimized.

Show comment
Hide comment
@anataliocs

anataliocs Jul 13, 2017

Thanks, updated the code sample.

Owner

anataliocs commented Jul 13, 2017

Thanks, updated the code sample.

@zhaozhi406

This comment has been minimized.

Show comment
Hide comment
@zhaozhi406

zhaozhi406 Jul 24, 2017

good solution!

zhaozhi406 commented Jul 24, 2017

good solution!

@viiicky

This comment has been minimized.

Show comment
Hide comment
@viiicky

viiicky Jul 29, 2017

Just a quick question: What do we need to have an interface MessageByLocaleService.java?

viiicky commented Jul 29, 2017

Just a quick question: What do we need to have an interface MessageByLocaleService.java?

@chetan7hallan

This comment has been minimized.

Show comment
Hide comment
@chetan7hallan

chetan7hallan Jun 26, 2018

at what location did you place messages_en_us-properties?
and what is significance of "classpath:locale/messages"?

chetan7hallan commented Jun 26, 2018

at what location did you place messages_en_us-properties?
and what is significance of "classpath:locale/messages"?

@WenderGalan

This comment has been minimized.

Show comment
Hide comment
@WenderGalan

WenderGalan Aug 27, 2018

Thank you brow!! You saved my life LOL

WenderGalan commented Aug 27, 2018

Thank you brow!! You saved my life LOL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment