I tried your solution, yet I do not understand class Resources.Localization. Where did it come from? My solution is described below and it was working with good resources.
Model:
using TestApp.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace TestApp.Models
{
public class LoginModel
{
public string Email { get; set; }
[Required]
[MinLength(5, ErrorMessageResourceName="Test", ErrorMessageResourceType=typeof(Resources))]
public string Password { get; set; }
}
}
ModelValidationFilterAttribute:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Net.Http;
namespace TestApp.Controllers
{
public class ModelValidationFilterAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
}
Two resource files, one general Resources.resx, containing string key/value Test/something; the other one Resources.de-DE.resx, containing string key/value Test/something_DE.
Using fiddler I sent this:
Header:
User-Agent: Fiddler
Host: localhost:63315
Content-Length: 37
Content-Type: application/json
Body:
{Email:"text@test.com", Password:"a"}
Response was string:
HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcV29ya1xDU1xGYkJpcnRoZGF5QXBwXEZiQmRheUFwcDJcYXBpXGxvZ2lu?=
X-Powered-By: ASP.NET
Date: Fri, 28 Jun 2013 14:56:54 GMT
Content-Length: 83
{"Message":"The request is invalid.","ModelState":{"model.Password":["something"]}}
For de-DE, request header was:
User-Agent: Fiddler
Host: localhost:63315
Content-Length: 37
Accept-Language: de-DE
Content-Type: application/json
Response was:
HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcV29ya1xDU1xGYkJpcnRoZGF5QXBwXEZiQmRheUFwcDJcYXBpXGxvZ2lu?=
X-Powered-By: ASP.NET
Date: Fri, 28 Jun 2013 14:57:39 GMT
Content-Length: 86
{"Message":"The request is invalid.","ModelState":{"model.Password":["something_DE"]}}
As you can see, message was with "_DE" as it was read from localized resources file.
Is this something that you wanted to achieve?
Kind regards.
Edit: route configuration was
config.Routes.MapHttpRoute(
name: "LoginRoute",
routeTemplate: "api/login",
defaults: new { controller = "Login", action = "PostLogin" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
);
Web.config had next section added:
<system.web>
...
<globalization culture="auto" uiCulture="auto" />
...
</system.web>