ModelState ErrorMessage文化本地化

时间:2014-10-09 07:42:47

标签: asp.net asp.net-mvc localization culture defaultmodelbinder

ModelState Validation Culture on HttpPost

Error messages from ModelState not get localized

我已经阅读了这些问题但他们并没有解决我的问题。

我的问题:ModelState验证errorMessages总是英文。本地化不起作用。

LocalizationAttribute.cs文件

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());
    }
}

我需要在模型状态错误开始之前设置文化,但我不知道该怎么做

1 个答案:

答案 0 :(得分:1)

LocalizationAttribute无法达到您想要的效果,因为在Thread.CurrentThread.CurrentUICulture方法中同时设置Thread.CurrentThread.CurrentCultureOnActionExecuting(ActionExecutingContext filterContext)时为时已晚。

OnActionExecuting被解雇时,程序集文化已经设置了ModelState错误。

解决方案1 ​​

覆盖基本控制器中的Initialize(System.Web.Routing.RequestContext requestContext)方法,并在Thread.CurrentThread.CurrentUICultureThread.CurrentThread.CurrentCulture中设置所需的文化。

解决方案2

覆盖Application_BeginRequest(object sender, EventArgs e)中的Global.asax,并在Thread.CurrentThread.CurrentUICultureThread.CurrentThread.CurrentCulture中设置所需的文化。