I'm having some problems with localized custom messages. I already have a AppMessages interface which extends com.google.gwt.i18n.client.Messages and StandardValidationMessages. The AppMessages interface has a corresponding AppMessages.properties (standard GWT setup).
How can I solve this problem? Do I need a separate AppValidationMessages class which extends ValidationMessages and overrides getPropertyName and/or getCustomMessage, if so, what would such a class look like?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You need to extend ValidationMessages and there you implement your own StandardValidationConstants.
Below is a snippet of how it is made in the Showcase:
publicclassShowcaseValidationMessagesextendsValidationMessages{privateShowcaseValidationConstantsmsg=GWT.create(ShowcaseValidationConstants.class);publicStringgetPropertyName(StringpropertyName){if(propertyName.equals("positiveInteger"))returnmsg.positiveInteger();elseif(propertyName.equals("integerInRangeMinus5000Plus5000"))returnmsg.integerInRangeMinus5000Plus5000();return"unknown property name";}publicShowcaseValidationConstantsgetPropertyNameConstants(){returnmsg;}publicStringgetDescriptionMessage(Stringkey){if(key.equals("positiveIntegerHelp"))returnmsg.positiveIntegerHelp();elseif(key.equals("integerInRangeMinus5000Plus5000Help"))returnmsg.integerInRangeMinus5000Plus5000Help();return"Unknown key: "+key;}@OverridepublicStringgetCustomMessage(Stringkey,Object...parameters){if(key.equals("regexNotMatched"))returnMessageInterpolator.merge(msg.regexNotMatched(),parameters);returnnull;//return"localization not found for key: "+key;}}
In the previous example the checks are hardcoded you can however implement a class of type MessagesWithLookup which is introduced by gwt-vl to dynamize this process like this:
With this method you save yourself from the if/else cascades in the previous example, you only need to make sure, that the key in the LookupMessages are the same than the keys that you feed to the ValidationProcessor instance.
As GWT does have ConstantsWithLookup but not MessagesWithLookup I needed to implement this class and thats why you additionally need to create a rebind rule, so that your custom MessagesWithLookup classes can be created by the GWT compiler for each possible language. You do this by putting the following into your *.gwt.xml file:
Thanks for your informative reply, however, I can not get it to work properly.
I tried your last approach by implementing MessagesWithLookup and overriding getCustomMessage in my subclass of ValidationMessages. The method getCustomMessage does not seem to be called at all when calling validate() on the validator.
Take a look at here for the ValidationMessages reference. There you can see that there also exists a getPropertyName method, which in your case you will be invoked with the key "test". In this method should return the name of the specified input (This is only necessary if you want your actions to be able to localize the name of the input field and can be toggled with "withPropertyName" in the LabelTextAction, default is to use property name localization). So to turn it off you could use "new LabelTextAction(errorLabel, ":", false).
The real problem with your code is that you are using a standard validator that ships with gwt-vl. Thesse validators all use the StandardValidationMessages associated with a ValidationMessages instance. To get the getCustomMessage method to be invoked you needed to a)Create an own Validator that not uses the standard keys or b) on the server side do a ServerValidation.exception("yourCustomKey", "yourPropertyName";
So to wrap it up to make your test working you should just do "new LabelTextAction(errorLabel, ":", false)" instead of "new LabelTextAction(errorLabel)". This will get rid of the error. For testing custom validation messages you will have to resort to the one or both of the two options above.
Regards, Anatol
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm having some problems with localized custom messages. I already have a AppMessages interface which extends com.google.gwt.i18n.client.Messages and StandardValidationMessages. The AppMessages interface has a corresponding AppMessages.properties (standard GWT setup).
However, the following code does not work:
How can I solve this problem? Do I need a separate AppValidationMessages class which extends ValidationMessages and overrides getPropertyName and/or getCustomMessage, if so, what would such a class look like?
Hi!
You need to extend ValidationMessages and there you implement your own StandardValidationConstants.
Below is a snippet of how it is made in the Showcase:
In the previous example the checks are hardcoded you can however implement a class of type MessagesWithLookup which is introduced by gwt-vl to dynamize this process like this:
Create your own MessagesWithLookup subclass:
You need to create LookupMessages.properties (normal GWT i18n way).
Then you can do this in your ValidationMessages subclass:
With this method you save yourself from the if/else cascades in the previous example, you only need to make sure, that the key in the LookupMessages are the same than the keys that you feed to the ValidationProcessor instance.
As GWT does have ConstantsWithLookup but not MessagesWithLookup I needed to implement this class and thats why you additionally need to create a rebind rule, so that your custom MessagesWithLookup classes can be created by the GWT compiler for each possible language. You do this by putting the following into your *.gwt.xml file:
Hope this clears things up for you, if you have further questions don't hesitate to ask!
Regards, Anatol
Thanks for your informative reply, however, I can not get it to work properly.
I tried your last approach by implementing MessagesWithLookup and overriding getCustomMessage in my subclass of ValidationMessages. The method getCustomMessage does not seem to be called at all when calling validate() on the validator.
Code:
Then I get the following message: no property localization given for key: test: Not a whole number
I'm using gwt-vl-2.0b-with-hibernate, if that matters.
Hi!
Take a look at here for the ValidationMessages reference. There you can see that there also exists a getPropertyName method, which in your case you will be invoked with the key "test". In this method should return the name of the specified input (This is only necessary if you want your actions to be able to localize the name of the input field and can be toggled with "withPropertyName" in the LabelTextAction, default is to use property name localization). So to turn it off you could use "new LabelTextAction(errorLabel, ":", false).
The real problem with your code is that you are using a standard validator that ships with gwt-vl. Thesse validators all use the StandardValidationMessages associated with a ValidationMessages instance. To get the getCustomMessage method to be invoked you needed to a)Create an own Validator that not uses the standard keys or b) on the server side do a ServerValidation.exception("yourCustomKey", "yourPropertyName";
So to wrap it up to make your test working you should just do "new LabelTextAction(errorLabel, ":", false)" instead of "new LabelTextAction(errorLabel)". This will get rid of the error. For testing custom validation messages you will have to resort to the one or both of the two options above.
Regards, Anatol