Localization inside ViewModel

Topics: Localization
Mar 1, 2013 at 2:00 PM
Edited Mar 1, 2013 at 2:01 PM
Hi guys,

I have a noob question regarding Orchard Localization:

I have a ViewModel inside my Orchard module that implements IValidatableObject interface. My problem is that I can't localize the validation messages:
public Localizer T { get; set; }

public LastMinuteVisitsModel()
{
      this.T = NullLocalizer.Instance;
}

if (this.RegionId <= 0)
{
    yield return new ValidationResult(T("Please select Region").Text);
}
Thanks in advance for your help!
Developer
Mar 1, 2013 at 9:53 PM
Edited Mar 1, 2013 at 9:54 PM
The problem is that your view model is instantiated either manually or using the DefaultModelBinder.
Although I am not sure, I don't think the DefaultModelBinder will inject a Localizer when you expose a public Localizer T (although you could surely try).
If it doesn't then perhaps it would be interesting to think about implementing such a model binder.

Alternatively, move your validation logic out of the view model and into the controller.
Mar 3, 2013 at 4:39 PM
I have the same issue, and I did not find the alternative where I do the validation in the controller nice, so I ended ut using this metod: http://orchard.codeplex.com/discussions/434990

Did you by any chance find a better way? Perhaps you do it through the DefaultModelBinder. If you did please share :-)
Mar 6, 2013 at 12:35 PM
Edited Mar 6, 2013 at 12:37 PM
Hi again,

sfmskywalker is right: DefaultModelBinder doesn't inject the Localzier. To avoid that, I instatiated manually my model inside my action from the controller and then pass the controller's localizer.
        [HttpPost]
        [ValidateAntiForgeryTokenOrchard]
        public ActionResult SearchLastMinuteVisits()
        {
            LastMinuteVisitsModel filters = new LastMinuteVisitsModel();
            filters.T = this.T;
             ...
         }

Thanks again for your time!