Internationalize variable in controllers?

0

Good, I would like to internationalize a variable in a controller. I have a form in which I fill some data and depending on the data I enter in the form I want to give value to a property of the entity that I show the form. I put you in situation, I have a form of an entity that is applicationFor (request) in which the client can put a credit card and if it is expired would insert the property reasonWhy "your card has been canceled" but in a way internationalized.

Controller that receives the submit of the form.

    @RequestMapping(value = "/enter", method = RequestMethod.POST, params = "enter")
public ModelAndView enter(@Valid ApplicationFor applicationFor, BindingResult binding) {
    ModelAndView result;
    if (binding.hasErrors())
        result = this.createEditModelAndViewCreditCard(applicationFor, null);
    else
        try {
            this.applicationForService.enter(applicationFor);

            result = new ModelAndView("redirect:list.do");
        } catch (final Throwable oops) {
            result = this.createEditModelAndViewCreditCard(applicationFor, "applicationforcreditError.commit.error");
        }
    return result;
}

Here the service that answers the request that is where I check the credit card and put in the property reasonWhy what I would like to internationalize:

public ApplicationFor enter(final ApplicationFor applicationFor) {
    Assert.notNull(applicationFor);
    Assert.isTrue(applicationFor.getStatus().equals("DUE"));
    ApplicationFor result;

    if (this.checkCreditCard(applicationFor.getCreditCard())) {
        applicationFor.setStatus("ACCEPTED");
        applicationFor.setReasonWhy("");
    } else {
        applicationFor.setStatus("REJECTED");
        applicationFor.setReasonWhy("Your credit card is expired");
    }
    result = this.applicationForRepository.save(applicationFor);
    return result;
} 
    
asked by José Joaquín Rodríguez Pérez 21.12.2017 в 17:53
source

1 answer

1

Having the internationalization already implemented. In the controller you can access the messages in the following way:

@Autowired
private MessageSource messageSource;

private void miFuncion(){
    Locale currentLocale = LocaleContextHolder.getLocale();
    String reasonWhy = messageSource.getMessage("reason.why", new Object[] {}, currentLocale);  
}
    
answered by 27.12.2017 в 08:49