Announcement Announcement Module
Collapse
No announcement yet.
Roo, JSR-303 Validations and Localization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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


      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

      Comment


      • #4
        Hi Ken,

        Glad it's working for you now!

        -Stefan

        Comment


        • #5
          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

          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

          Comment


          • #6
            resolved

            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

            Comment


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

              -Stefan

              Comment


              • #8
                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?

                Comment


                • #9
                  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

                  Comment


                  • #10
                    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?

                    Comment

                    Working...
                    X