我对 MVC 应用程序的本地化感到疯狂。
在我最近的一个问题之后,我遵循了这种方法:
- 语言存储在 Session["lang"]
- 每个控制器都继承自我自己的BaseController,它覆盖了OnActionExecuting,并在这个方法中读取Session并设置CurrentCulture和CurrentUICulture
这很好用,直到数据注释层出现。似乎它在动作本身执行之前被调用,因此它总是以默认语言获取错误消息!
字段声明如下所示:
[Required(ErrorMessageResourceName = "validazioneRichiesto", ErrorMessageResourceType = typeof(Resources.Resources))]
public string nome { get; set; }
那么,有什么合理的地方可以拨打电话吗?
我在 Controller 构造函数中初始化了 Data Annotation Model Binder。
public CardController() : base() {
ModelBinders.Binders.DefaultBinder =
new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
}
因此,由于 Session 在控制器的构造函数中始终为空,并且在数据注释验证字段之后调用操作覆盖,我可以在哪里设置 CurrentCulture 和 CurrentUICulture 以获取本地化错误?
我尝试将 CurrentCulture 和 CurrentUiCulture 放入 Application_*(例如 Application_AcquireRequestState 或 Application_PreRequestHandlerExecute)似乎没有任何效果......