1
votes

We are using the new GWT validation library in 2.5.

We are adding an aggregated list of violations to our screen. This list must display the localized field name.

@MyNotNull(foo= "Stage")
public String getStage();

Localized message needs to display

"Stage is a required field"

The message in MyValidationMessages.properties reads

{foo} is a required field

Note that annotations do not allow non-constant values to be assigned to attributes. So we have to get the locale value somehow at design time :/

This will not work

@MyNotNull(foo = injector.getLocale().errorMessage()) 
public String errorMessage()

How do I use localeKey to look up the locale in the locale files since the property requires a constant?

1
Please add relevant code snippets. - appbootup

1 Answers

0
votes

The solution is to

  • add something like FieldLocale.properties this is a constants lookup
  • Add an attribute to your annotation like localeKey
  • Iterate your ConstraintViolation collection
  • Use something like the below to get the attribute value
  • Look up the localized value in your FieldLocale.properties file
  • Copy the violation and change the message to the localized version

    protected String getAttributeValue(ConstraintViolation violation, String key) { ConstraintDescriptor descriptor = violation.getConstraintDescriptor();

        if (descriptor.getAttributes().containsKey(key))
            return (String) descriptor.getAttributes().get(key);
    
        return null;
    }
    
    protected ConstraintViolation<T> copyMessage(ConstraintViolation<T> violation, String message) {
        return ConstraintViolationImpl.<T> builder() //
                .setConstraintDescriptor(violation.getConstraintDescriptor()) //
                .setInvalidValue(violation.getInvalidValue()) //
                .setLeafBean(violation.getLeafBean()) //
                .setMessage(message) //
                .setMessageTemplate(violation.getMessageTemplate()) //
                .setPropertyPath(violation.getPropertyPath()) //
                .setRootBean(violation.getRootBean()) //
                .setRootBeanClass(violation.getRootBeanClass()) //
                .build();
    }