I want to display model error message in spanish and I have defined those string in resourse files. I have done the same for other string on the page using razor syntax but the ones from ViewModel annotation are not being picked.
It is actually picking the default values - English. So my guess is that may be the language/culture was not detected, but other string on the page are displayed in spanish
//Spanish: El campo {0} se requiere
//English: The {0} field is required <--- this comes out always irrespective of set language
[Required(ErrorMessageResourceName = "ErrorMessage_Required",
ErrorMessageResourceType = typeof(GlobalResources.Resources))]
[Display(Name = "CardNumber", ResourceType = typeof(GlobalResources.Resources) )]
public string CardNumber { get; set; }
I set the language in my Controller
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
HttpCookie cookie = Request.Cookies["lang"];
string lang = cookie != null ? cookie.Value : "en-US";
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}
How do I extend the culture settings to ViewModels?
Update A similar post: MVC3 globalization: need global filter before model binding
Update
Changing the preferred language in my browser settings made it work. This means the model attributes is using this settings which is not affected by System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
. Is there a way to make this happen? - Still searching....
Update: Moving the code to Application_AcquireRequestState
seems to solve it.
protected void Application_AcquireRequestState()
{
HttpCookie cookie = Request.Cookies["lang"];
string lang = cookie != null ? cookie.Value : "en-US";
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}
The explanation I got, also found in the link posted in this question, that it was too late for the model to make use of the culture being set in controller overridden method as the binding had already occurred before the method is called. This link was helpful