I need to send email confirmation, so now I have to localize sent message. I have initialized i18n in spring and now it works perfectly in jsp pages, but how can I use it in my controllers?
Overview. MessageSource is a powerful feature available in Spring applications. This helps application developers handle various complex scenarios with writing much extra code, such as environment-specific configuration, internationalization or configurable values.
Explanation: The default locale resolver used by Spring is AcceptHeaderLocaleResolver.
If you are using annotated controllers you can autowire the MessageSource and add the locale of the request like this:
@Controller @Scope("request") public class MailController { @Autowired private MessageSource messageSource; @RequestMapping(value = "/mail/send", method = RequestMethod.GET) public ModelAndView sendEmail(Locale locale) { String[] args = { "Mr.", "X" }; // E.g. message.code="Dear {0} {1}" String mailmessage = messageSource.getMessage("message.code", args, locale); // Do something return new ModelAndView(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With