10

I made a MVC based website using JSF 2.0 and RichFaces 4. Every input text validation is been done using bean validation annotations. I am using Hibernate Validator as bean validation implementation.

How can I display a localized message?

If I use

@NotNull(message="<h:outputText value=\"#{msg['Mymessage']}\" />")

then it literally displays <h:outputText value="#{msg['Mymessage']}" /> as message.

enter image description here

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Heetola
  • 5,791
  • 7
  • 30
  • 45

2 Answers2

33

You should and can not put JSF tags in the message. Also, JSF's own resource bundle won't be used to resolve localized validation messages. JSR303 bean validation is a completely separate API unrelated to JSF.

To internationalize JSR303 bean validation messages, you need to create a separate ValidationMessages.properties file in the classpath root which can be localized by ValidationMessages_xx_XX.properties files.

E.g.

ERVNomView=Your message here

Which is then to be specified with {key} syntax.

@NotEmpty(message="{ERVNomView}")

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

For those of you having the same problem as me (the solution given by @BalusC wasn't working with many different properties files for many languages), the thing to do is to name all of the properties files in the following pattern: ValidationMessages_xx.properties

I don't know why, but the pattern ...xx_XX... wasn't working for me.

jogproof
  • 43
  • 8
  • 1
    The pattern `xx_XX` is for region specific locales, e.g. British English is `en_UK`, whereas `en` just stand for English in general. You should always have the general variant to fall back to, then it will work. – Tilo Jun 10 '16 at 15:39