1

In my web application, I handle errors with annotations. Everything works fine and I can use custom messages via the "message" parameter.

@Digits(fraction = 0, integer = 3, message="my custom error message...")
private String price;

Now I'm trying to internationalize this message with a .properties files, but I certainly miss something and I can't get it to work.

My spring config :

<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basenames" value="classpath:i18n/messages, classpath:i18n/errors" />
    <beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>

<beans:bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <beans:property name="validationMessageSource">
        <beans:ref bean="messageSource" />
    </beans:property>
</beans:bean>

<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
    <beans:property name="defaultLocale" value="fr" />
</beans:bean>

My new bean :

@Digits(fraction = 0, integer = 3)
private String price;

My "errors_fr.properties" file. I've already tried everything :

Digits.myBean.myNestedBean.price = my custom error message...
Digits.myNestedBean.price = my custom error message...
javax.validation.constraints.Digits.myNestedBean.price = my custom error message...

I always get the same generic message from spring, it's like as spring doesn't detect my .properties file. By the way, the message keys above can be found in the BindingResult object when debugging.

What am I missing here ?

Notice that I already have internationalized messages in my jsp's (in the "messages_fr.properties" file) and they work fine.

1 Answer 1

5

I had a similar problem in my application, and I hope that this can help you.

As discussed in this thread, http://forum.springsource.org/showthread.php?73240-Roo-JSR-303-Validations-and-Localization, you need to:

  1. define the error messages referred by the annotation in file ValidationMessages.properties
  2. in your annotation, refer to the error message key enclosed in curly brackets:

@Digits(fraction = 0, integer = 3, message="{message.key}")

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes you're absolutely right ! I had already tried to put a ValidatorMessages.properties in my classpath but it didn't work. After reading your link I understood why : I called it ValidatorMessages_fr.properties, and I had to name it "ValidatorMessages_fr_FR.properties". So thanks ! However, before to check your answer, I wanted to know, is there some way to call the file "errors_fr.properties" (instead of "ValidatorMessages_fr_FR.properties") like I just wanted to do in the beggining ?
I haven't tried this one, but seems like the last comment in thread forum.springsource.org/… explains how to keep messages in file messages.properties. Let me know if this works for you and I'll try it as well. :)
Yes, that seems to work ! However, it would be nice to use only xml configuration (and I always don't understand why my initial configuration doesn't work). This is a different matter though. Thanks !
How can you send the parameters "fraction" and "integer" to be part of the message.key? I tried with {fraction} and {integer}, and also with {0} and {1}. But the parameters are not shown on the message. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.