Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I translate text inside controller in Spring Framework 3?

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?

like image 879
newbie Avatar asked May 04 '10 11:05

newbie


People also ask

What is MessageSource in Spring?

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.

What is LocaleResolver used by Spring MVC?

Explanation: The default locale resolver used by Spring is AcceptHeaderLocaleResolver.


1 Answers

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();     } } 
like image 95
Daff Avatar answered Oct 02 '22 18:10

Daff