ADH
ADH

Reputation: 3071

ASP.Net MVC Validation Localization Not Overriding Browser Default Language

I have localization working as expected for labels, but cannot seem to figure out why the validation messages are not being localized. If I change my browser default language to "es-ES", the validation messages are in Spanish. But, the application should be using the language set in our local database. For example, in the code below, the label for Impacts is being localized to Spanish, but the ProductID error message is displaying in English. The Culture is being set at the beginning of the controller action using a cookie.

I also noticed if I set the language to Spanish in the web.config that the validation messages are in Spanish.

Here is the current code that works correctly for the label, but not for the validation message.

What I want is for the validation error message to be in Spanish like the label.

Web.Config:

<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true" />

View Model:

[Required(ErrorMessageResourceName = "errProduct", ErrorMessageResourceType = typeof(Resources.ProductSupport.addcallText))]
public Int32? ProductID { get; set; }

[Display(Name = "lblEffect", ResourceType = typeof(Resources.ProductSupport.addcallText))]
public IEnumerable<SelectListItem> Impacts { get; set; }

View:

@Html.ValidationMessageFor(x => x.ProductID)

@Html.LabelFor(x => x.Impacts)

Upvotes: 2

Views: 1152

Answers (1)

ADH
ADH

Reputation: 3071

Setting the culture in the Global.asax.cs file seems to be working.

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    LanguageHelper.SetCulture(null);
}

Upvotes: 2

Related Questions