asp.netasp.net-mvclocalizationculturedefaultmodelbinder

ModelState ErrorMessage Culture Localization


ModelState Validation Culture on HttpPost

Error messages from ModelState not get localized

I have read these questions and they didn't solve my problem.

My problem: ModelState Validation errorMessages are always in English. Localization does not work.

LocalizationAttribute.cs File

public class LocalizationAttribute : ActionFilterAttribute
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.RouteData.Values["lang"] != null &&
            !string.IsNullOrWhiteSpace(filterContext.RouteData.Values["lang"].ToString()))
        {
            // set the culture from the route data (url)
            string lang = filterContext.RouteData.Values["lang"].ToString();
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(lang);
            CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(lang);
            filterContext.HttpContext.Session.Add("Culture", new CultureInfo(lang));
        }
        else
        {
            // get the browser culture
            string langHeader = filterContext.HttpContext.Request.UserLanguages[0];
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(langHeader);
            filterContext.RouteData.Values["lang"] = langHeader;
        }
        base.OnActionExecuting(filterContext);
    }
}

Web.config

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

Global.asax.cs

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        AutoMapperConfiguration.Configure();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        // Set the view engine
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new MyCustomViewEngine(SiteSettings.Theme));
    }
}

FilterConfig.cs

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new LocalizationAttribute());
    }
}

I need to set culture before modelstate error start, but I don't know how to do that


Solution

  • You will not achieve what you want by LocalizationAttribute because, it's too late when you set both Thread.CurrentThread.CurrentUICulture or Thread.CurrentThread.CurrentCulture in OnActionExecuting(ActionExecutingContext filterContext) method.

    When OnActionExecuting get fired the ModelState errors are already set by the assembly culture.

    Solution 1

    Override Initialize(System.Web.Routing.RequestContext requestContext) method in a base controller and set the wanted culture in Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture.

    Solution 2

    Override Application_BeginRequest(object sender, EventArgs e) in the Global.asax and set the wanted culture in Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture.