Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I realize this is an old question, but wanted to add a bit. I found myself in the same situation and there doesn't appear to be any documentation/blogumentation on this topic. Nevertheless, I figured out a way to use a custom resource provider, with one caveat. The caveat is that I'm in an MVC application so I still have <code>HttpContext.GetLocalResourceObject()</code> available. This is the method that asp.net uses to localize items. The absence of the resource object doesn't stop you from writing our own solution, even if its a direct query of the DB tables. Nevertheless, I thought it was worth pointing out.</p> <p>While I'm not terribly happy with the following solution, it seems to work. For each validation attribute I want to use I inherit from said attribute and overload the IsValid(). The decoration looks like this:</p> <pre><code>[RequiredLocalized(ErrorMessageResourceType= typeof(ClassBeginValidated), ErrorMessageResourceName="Errors.GenderRequired")] public string FirstName { get; set; } </code></pre> <p>The new attribute looks like this:</p> <pre><code>public sealed class RequiredLocalized : RequiredAttribute { public override bool IsValid(object value) { if ( ! (ErrorMessageResourceType == null || String.IsNullOrWhiteSpace(ErrorMessageResourceName) ) ) { this.ErrorMessage = MVC_HtmlHelpers.Localize(this.ErrorMessageResourceType, this.ErrorMessageResourceName); this.ErrorMessageResourceType = null; this.ErrorMessageResourceName = null; } return base.IsValid(value); } } </code></pre> <p><b>Notes</b></p> <ul> <li>You need to decorate your code with the derived attribute, not the standard one</li> <li>I'm using ErrorMessageResourceType to pass the type of the class being validated. By that I mean if I'm in a customer class and validating the <i>FirstName</i> property I would pass <i>typeof(customer)</i>. I'm doing this because in my database backend I'm using the full class name (namespace + classname) as a key (the same way a page URL is used in asp.net). <ul> <li><i>MVC_HtmlHelpers.Localize</i> is just a simple wrapper for my custom resource provider </li> </ul></li> </ul> <p>The (semi-stolen) helper code looks like this ....</p> <pre><code>public static string Localize (System.Type theType, string resourceKey) { return Localize (theType, resourceKey, null); } public static string Localize (System.Type theType, string resourceKey, params object[] args) { string resource = (HttpContext.GetLocalResourceObject(theType.FullName, resourceKey) ?? string.Empty).ToString(); return mergeTokens(resource, args); } private static string mergeTokens(string resource, object[] args) { if (resource != null &amp;&amp; args != null &amp;&amp; args.Length &gt; 0) { return string.Format(resource, args); } else { return resource; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload