I want to put validation messages used by Bean Validation (JSR 303) ex.:
javax.validation.constraints.AssertFalse.message=must be false
javax.validation.constraints.AssertTrue.message=must be true ...
into a database so that when an administrator adds a new language he can adds the translations for that language.
I know how to access a resource bundle mapped from a database but I can't figure out how to extend/customize the bean validation classes so they can access the validation messages from a database...
Is it possible to obtain what I want ?
Many thanks in advance for driving me into the right direction.
HERE THE SOLUTION (I don't know if this is the best solution but it works...):
As suggested by @gastaldi I've created an implementation on MessageInterpolator interface:
package giates.validation;
import com.infomaxgroup.adaecommerce.bundles.DatabaseResourceBundle;
import java.util.Locale;
import java.util.Map;
import javax.validation.MessageInterpolator;
public class LocaleMessageInterpolator implements MessageInterpolator {
protected final String BRACE_OPEN = "\\{";
protected final String BRACE_CLOSE = "\\}";
@Override
public String interpolate(String message, Context context) {
return interpolate(message, context, Locale.ITALIAN);
}
@Override
public String interpolate(String message, Context context, Locale locale) {
DatabaseResourceBundle bundle = new DatabaseResourceBundle(locale);
String messageKey = context.getConstraintDescriptor().getAttributes().get("message").toString();
message = bundle.getString(messageKey.replaceAll(BRACE_OPEN, "").replaceAll(BRACE_CLOSE, ""));
Map<String, Object> attributes = context.getConstraintDescriptor().getAttributes();
for (String key : attributes.keySet()) {
String value = attributes.get(key).toString();
key = BRACE_OPEN + key + BRACE_CLOSE;
message = message.replaceAll(key, value);
}
return message;
}
}
then I've created META-INF/validation.xml and added the customized messageinterpolator:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
<message-interpolator>giates.validation.LocaleMessageInterpolator</message-interpolator>
</validation-config>
as soon as I post a model with failed validations the interpolator calls DatabaseResourceBundle and creates the resulting message...
All works great !