Validators' messages personalization

Topics: Developer Forum
May 9, 2007 at 5:24 PM
The problem is as follows:
- there are two users of the same application (web application)
- one of them is English, the other, let's say, Polish
- both do the same things but the first must get messages like: "The date is required" and the second - "Data jest wymagana"
- but as I can see, all rules are cached at the level of the whole application - they are common to all users
- and I can't see any method to change rule's message just before generating validators
Is there any solution of this problem? It's the crucial issue for my application.
Regards
Tomasz
Coordinator
May 9, 2007 at 11:54 PM
Edited May 10, 2007 at 3:42 AM
Tomasz

One possible solution for this problem is through the use of a RuleSet. A rule set is a way of only calling a specific group of rules based on a string identifier. All Rules, base classes and helper classes support this feature. So you could have a different RuleSet for each language (or country).

There a two common type of validation which I will talk about separately

Internal validation

Description:
When a business object handles it own validity internally. The two ways of doing this are by inheriting from the base validation classes (see \Examples\ExampleLibraryCSharp\BusinessBaseClasses\NotifyValidatableBaseSample.cs) or by having your own PropertyValidationManager inside your business class (see \Examples\ExampleLibraryCSharp\PropertyValidationManager\CustomClassSample.cs)
Approach:
Pass the language into your constructor as an enumeration, using an enumeration will stop people passing in invalid countries. Convert that enumeration to a string and pass it to the RuleSet parameter in either the base constructor or the PropertyValidationManager constructor, depending on which of the above scenarios you are using.
Then, optionally, create a factory for creating your business objects. The factory can determine the current language and return the instantiated object. Alternatively you could do this in the business object constructor.

External validation

Description:
When a business object is not aware of it own validity and is validated by a external (to the business object) PropertyValidationManager. (see \Examples\ExampleLibraryCSharp\PropertyValidationManager\ExternalSample.cs)
Approach:
Create a factory for creating PropertyValidationManager(s). The factory can determine the current language and return an appropriately instantiated PropertyValidationManager.

Duplicate Rules

Then you need to duplicate all your rules for different languages.
So instead of having
   [RequiredDateTimeRule(ErrorMessage = "The date is required.")] 
    public DateTime Data
    {
      get
      {
        return data;
      }
      set
      {
        data = value;
      }
    }
You would use
[RequiredDateTimeRule(RuleSet = "English", ErrorMessage = "The date is required.")]
[RequiredDateTimeRule(RuleSet = "Polish", ErrorMessage = "Data jest wymagana.")] 
    public DateTime Data
    {
      get
      {
        return data;
      }
      set
      {
        data = value;
      }
    }

Hope all that makes sense

Regards

Simon
May 11, 2007 at 7:28 AM
Thanks for the explanations.
It means that I have to have copies of validation logic for all supported languages. And if I want to change anything in this logic I must do it as many times as many languages I support :-( No, it's not a good solution. It seems I must create my own "validation framework" - not so universal and robust as yours but strictly fit to my needs. I've started doing this - and it looks quite nice.
Regards
Tomasz
Coordinator
May 27, 2007 at 11:43 AM
Edited May 27, 2007 at 11:44 AM
Tomasz
On a recent check-in I have added a feature that allows you to intercept the request for an error message. This allows you to programmatically decide what message to supply. Have a look at “Validation\QuickStarts\QSWindowsApplicationCSharp\MultiLanguageForm.cs” for an example usage. I have not fully document this feature yet but i though the code might give you some ideas for you own implementation.
The MultiLanguageForm.cs draws its error messages from an xml file base on the current culture of a user.

Regards
Simon