JSR303 localized messages with RequestFactory

194 views
Skip to first unread message

Peter Quiel

unread,
Jan 24, 2012, 5:22:50 AM1/24/12
to Google Web Toolkit
When the RequestFactory validates my domain objects using a jsr303
validator it gets localized contraint violation message and a message
key. I debuged the code and on the server side the default locale is
used no matter what locale is used on the client side. I did not find
any locale settings in the payload either.

Are there any examples how to treat localization in comibation with
RequestFactory and BeanValidation? Should the messages be translated
on the client or server side? What are the best practices?

Thanks!

Handw

unread,
Feb 14, 2012, 1:11:10 PM2/14/12
to google-we...@googlegroups.com
+1.
I also want to know.

Jens

unread,
Feb 14, 2012, 2:33:11 PM2/14/12
to google-we...@googlegroups.com
Just googled a bit because I have never thought about it and based on the search results I think I would try it the following way (untested): 

1.) Send the current client side locale as a HTTP header (e.g. X-GWT-LOCALE) to the server using a custom RequestTransport for RequestFactory (extend DefaultRequestTransport and add the header).

2.) On server side create a new custom MessageInterpolator that can overwrite the locale:

public class GwtClientLocaleMessageInterpolator implements MessageInterpolator {

  public GwtClientLocaleMessageInterpolator(MessageInterpolator default, Locale gwtClientLocale) {
    //remember parameters as fields
  }

  //implement methods by delegating to the default interpolator but overwriting the locale

}

3.) On server side create a ServiceLayerDecorator for RequestFactory (extend RequestFactoryServlet and provide your decorator through a super call in the constructor) and change the way the Validation will be done in ServiceLayerDecorator.validate(..):

MessageInterpolator defaultInterpolator = ValidatorFactory.getMessageInterpolator();
GwtClientLocaleMessageInterpolator interpolator = new GwtClientLocaleMessageInterpolator(defaultInterpolator, localeFromGwtClient);
Validator validator = ValidatorFactory.usingContext().interpolator(interpolator).getValidator();
validator.validate(....);


Hopefully you should now have messages based on the client GWT locale.

-- J.

André Salvati

unread,
May 10, 2012, 1:09:39 PM5/10/12
to google-we...@googlegroups.com
Hi Jens,

do you remember references for your tips??

Have anyone tested this solutions??

Thanks.

Jens

unread,
May 11, 2012, 4:24:33 AM5/11/12
to google-we...@googlegroups.com
No I don't know the exact references anymore. But I am pretty sure it should work this way. 

-- J.

André Salvati

unread,
May 11, 2012, 7:45:49 AM5/11/12
to google-we...@googlegroups.com
I've just translated messages to portuguese into a new ValidationMessages.properties file and put it into classpath. Hibernate Validator autodetects this new configuration.

Thanks.

Manu Botija

unread,
Oct 8, 2013, 1:13:47 PM10/8/13
to google-we...@googlegroups.com
Hi André, 

Would you mind sharing the code from your solution so that implements Jens idea? It would be great for newbies like me looking for a solution for this problem.

Thanks!
Manu

Manu Botija

unread,
Oct 8, 2013, 3:57:55 PM10/8/13
to google-we...@googlegroups.com
I have given it a go. Here is my working code

Now that I understand how this works, I wonder if there is a simpler solution for just setting the default Interpolator locale...

This guy here seems to have a solution but he is using Spring and grails which I am not.
Reply all
Reply to author
Forward
0 new messages