views:

85

answers:

1

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?

+1  A: 

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();
    }
}
Daff