I'm going crazy about localizing an MVC application.
After my recent question, I took this approach:
- The language is saved in the session ["lang"]
- Each controller inherits from my own BaseController, which overrides OnActionExecuting and in this method reads the session and sets CurrentCulture and CurrentUICulture
This works fine until the level of data annotation appears. It seems to be called before the action is executed, and therefore it always gets error messages in the default language!
Field declarations are as follows:
[Required(ErrorMessageResourceName = "validazioneRichiesto", ErrorMessageResourceType = typeof(Resources.Resources))]
public string nome { get; set; }
So, is there a reasonable place where I can call?
I initialize the data annotation model binding in my controller constructor.
public CardController() : base() {
ModelBinders.Binders.DefaultBinder =
new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
}
So, since Session is always null in the controller constructor, and the action override is called AFTER the data annotation has checked the fields , where can I set CurrentCulture and CurrentUICulture to get localized errors?
I tried putting CurrentCulture and CurrentUiCulture in Application_ * (e.g. Application_AcquireRequestState or Application_PreRequestHandlerExecute) seems to have no effect ...
source
share