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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With