0

ValidationMessages.properties

error.name.invalid= Inavlid name
error.name.invalid.spanish= some spanish text

How to use the Spanish validation message instead of English just for a single page in the application based on a property value?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user679526
  • 845
  • 4
  • 16
  • 39
  • 5
    Really? Why don't you use 1 file for English and 1 file for Spanish, both with identical keys, so that you can use the builtin localization facility, as recommended and demonstrated in the documentation and tutorials? – BalusC Sep 18 '14 at 21:34

1 Answers1

0

If you really would like to do this, you could create custom validators. For example:

@FacesValidator("yourValidator")
public class YourValidator implements Validator
{

  @Override
  public void validate(FacesContext context, UIComponent component, Object value)
  throws ValidatorException
  {
    ResourceBundle resourceBundle = ResourceBundle.getBundle("/ValidationMessages");
    String key = yourKey + (yourProperty ? "" : ".spanish");
    String message = resourceBundle.getString(key);
    FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
    throw new ValidatorException(facesMessage);
  }

}

See also:

Community
  • 1
  • 1
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • Custom validator might not be a good option. We have to write 10 or more custom validators for each field instead of a field level hibernate validator. – user679526 Sep 22 '14 at 20:54
  • Of course it's a lot of work. The prefered way of handling this is having a separate message bundle for Spanish as @BalusC already pointed out. – Jasper de Vries Sep 23 '14 at 07:58