ASP Net MVC - Localization of Validation Messages
I have found a solution to my problem here: http://afana.me/post/aspnet-mvc-internationalization.aspx
What i did was to remove my "Internationalization" Attribute and create a "BaseController" class that basically does the same thing as the "Internationalization" Attribute on each request.
Basecontroller:
public class BaseController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
//Localization in Base controller:
string language = (string)RouteData.Values["language"] ?? "de";
string culture = (string)RouteData.Values["culture"] ?? "DE";
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
return base.BeginExecuteCore(callback, state);
}
}
Localization works now for labels and validation messages.
Martin
Hello, I'm Martin a software developer from Germany. I started programming 3 years ago with C# and .Net.
Updated on June 13, 2022Comments
-
Martin almost 2 years
I ran into a problem with localization of validation messages in my asp net mvc 5 application.
I use this for localization:
Route Config:
[Internationalization] public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute("DefaultLocalized", "{language}-{culture}/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "", language = "de", culture = "DE" }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }
LocalizationAttribute:
class InternationalizationAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { string language = (string)filterContext.RouteData.Values["language"] ?? "de"; string culture = (string)filterContext.RouteData.Values["culture"] ?? "DE"; Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture)); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture)); } }
Example of model property:
[Display(Order = 0, ResourceType = typeof(resourceProjectName.ApplicationStrings), Name = "LabelText")] // this works [Required(ErrorMessageResourceType = typeof(resourceProjectName.ApplicationStrings), ErrorMessageResourceName = "ValidationText")] //this does not work public string Property { get; set; }
Html example:
This is inside a Html.BeginForm. The validation messages are shown after a POST request, if something is missing or not valid.
<div> <div class="form-group row"> <div> @Html.LabelFor(x => x.Property) </div> <div class="col-sm-6"> @Html.TextBoxFor(x => x.Property, new { @class = "form-control" }) </div> @if (ViewData.ModelState.Where(i => i.Key.Equals("Property")).Any(x => x.Value.Errors.Any())) { <div class="col-sm-6"> @Html.ValidationMessageFor(x => x.Property, "", new { @class = "alert-validation alert-danger" }) </div> } </div> </div>
Web.config:
<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"/>
Localization works for everything on the website except for validation messages.
It always shows the resource string that matches the browsers language settings, although the user navigated to e.g.: site.de/en-EN/test.
Is there a problem with the web.config? I tried to set enableClientBasedCulture to false, but the problem still occured.
Kind regards, Martin
-
Sudani over 6 yearsI had a similar issue using different article and it turned to be that I forgot to reference jquery-validate and jquery-validate-unobtrusive, hopefully this as well will help someone else.
-
Andrew Grothe over 5 yearsThis also worked for me. I'm using an attribute and it seems that some (not always oddly enough) models are validated in different parts of the request pipeline as this fixed the issue for some Identity view models where as other view models didn't' have the issue.