Results 1 to 10 of 10

Thread: Roo, JSR-303 Validations and Localization

  1. #1
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default Roo, JSR-303 Validations and Localization

    I have a beginner's question... Forgive me if it's in a FAQ somewhere.

    I set up a message source in my applicationContext.xml file:

    <!-- kjr added -->
    <bean id="messageSource"
    class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basenames">
    <list>
    <value>messages</value>
    </list>
    </property>
    </bean>

    I also added a messages.properties and messages_en.properties file in src/main/resources, and annotated my Entity with the following:

    @Column(length=2048)
    @Size(min = 10, message = "{description.required}")
    private String description;

    What's great is that the JSR-303 annotations are firing. What's not is that I've tried ${}, #{} and {} and none of them work. The ${} yells about groups (looks like it's parsing for attributes from the annotation parameters) and the # and {} ones are ignored. Does roo support localizing error messages yet, and if not, is it on the roadmap?

    Thanks and keep up the good work. Interesting framework and has lots of potential.

    Ken

  2. #2
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    Hi Ken,

    As you noted Roo uses the JSR 303 Bean Validation framework. As per section 4.3 of the JSR 303 specification you need to define a ValidationMessages.properties file in the resources root (ie src/main/resources) and then reference properties defined in there with simple curly brackets like this:

    Code:
    @NotNull(message="{pet.weight}")
    @Min(0L, message="Minimum weight should be {min}")
    private Float weight;
    Note, that you don't need to define the ValidationMessages.properties in a ResourceBundleMessageSource in your application context.

    Hope this helps.

    -Stefan

  3. #3
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    Stefan,

    Thank you for referring me to the RTFM of the JSR! This works great. I was thinking it would be integrated into the ResourceBundleMessageSource which led me astray.

    Thanks again,

    Ken


    Quote Originally Posted by Stefan Schmidt View Post
    Hi Ken,

    As you noted Roo uses the JSR 303 Bean Validation framework. As per section 4.3 of the JSR 303 specification you need to define a ValidationMessages.properties file in the resources root (ie src/main/resources) and then reference properties defined in there with simple curly brackets like this:

    Code:
    @NotNull(message="{pet.weight}")
    @Min(0L, message="Minimum weight should be {min}")
    private Float weight;
    Note, that you don't need to define the ValidationMessages.properties in a ResourceBundleMessageSource in your application context.

    Hope this helps.

    -Stefan

  4. #4
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    Hi Ken,

    Glad it's working for you now!

    -Stefan

  5. #5
    Join Date
    Dec 2009
    Posts
    11

    Default localization

    define ValidationMessages.properties works fine, however, I also defined ValidationMessages_zh_CN.properties, but app still shows me the value from ValidationMessages.properties, not from ValidationMessages_zh_CN.properties. by looking at jsr 303, "Message resolution and localization" is is a work in progress, is it still the case?

    thanks

    Quote Originally Posted by Stefan Schmidt View Post
    Hi Ken,

    As you noted Roo uses the JSR 303 Bean Validation framework. As per section 4.3 of the JSR 303 specification you need to define a ValidationMessages.properties file in the resources root (ie src/main/resources) and then reference properties defined in there with simple curly brackets like this:

    Code:
    @NotNull(message="{pet.weight}")
    @Min(0L, message="Minimum weight should be {min}")
    private Float weight;
    Note, that you don't need to define the ValidationMessages.properties in a ResourceBundleMessageSource in your application context.

    Hope this helps.

    -Stefan

  6. #6
    Join Date
    Dec 2009
    Posts
    11

    Default resolved

    Quote Originally Posted by bliu72 View Post
    define ValidationMessages.properties works fine, however, I also defined ValidationMessages_zh_CN.properties, but app still shows me the value from ValidationMessages.properties, not from ValidationMessages_zh_CN.properties. by looking at jsr 303, "Message resolution and localization" is is a work in progress, is it still the case?

    thanks
    Just downloaded ROO release RC4, this works. thanks

  7. #7
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    Glad to hear it works for you now! Thanks for the feedback.

    -Stefan

  8. #8
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    668

    Question

    Quote Originally Posted by Stefan Schmidt View Post
    As per section 4.3 of the JSR 303 specification you need to define a ValidationMessages.properties file in the resources root (ie src/main/resources)
    What if I want my validation error messages to be theme-specific like all my other messages?
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  9. #9
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    Andrew,

    If you mean by 'theme-specific' that you want to have locale specific translations - this should be supported by the JSR 303 spec. Hibernate validator actually ships with a couple of translations. If I remember right it is German and French. You should be able to just define a ValidationMessages_es_ES.properties for example to offer spanish. More information is in the JSR 303 spec.

    -Stefan
    Stefan Schmidt
    Software Engineer, Spring Roo
    SpringSource - a division of VMware
    twitter @schmidtstefan

  10. #10
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    668

    Question

    Quote Originally Posted by Stefan Schmidt View Post
    Andrew,

    If you mean by 'theme-specific' that you want to have locale specific translations - this should be supported by the JSR 303 spec. Hibernate validator actually ships with a couple of translations. If I remember right it is German and French. You should be able to just define a ValidationMessages_es_ES.properties for example to offer spanish. More information is in the JSR 303 spec.

    -Stefan
    Actually Stefan I was thinking of Spring MVC themes, you know like how Roo apps come with the "standard" and "alt" themes by default. We have a real-world requirement for an app to be delivered in multiple themes, each with its own:

    • images
    • CSS files
    • messages

    As you will know, the first two of these are easily achieved by setting the relevant properties in theme_name.properties and its localised variants such as theme_name_es.properties (for Spanish), then accessing those properties using the <spring:theme> tag. Most screen text can likewise be localised on a per-theme level by adding translations to those properties files, for example in standard.properties:
    Code:
    greeting=Hello
    And in standard_es.propertie:
    Code:
    Hola
    Furthermore, the "alt" theme (or any other theme) can have its own separate theme-specific translations for those same languages.

    This is all at odds with the JSR-303 validation messages, which are indeed localisable (if that's a word), but not per theme; for a given user locale, the app will use the same set of validation error messages regardless of the active theme.

    Does that make sense?
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •