Localization of data annotations using IStringLocalizer
<p>I trying to implement localization in .net core 1.0 application using <code>IStringLocalizer</code>. I am able to do the localization for the view for which I have written something like this</p>

<pre><code>    private readonly IStringLocalizer&lt;AboutController&gt; _localizer;

    public AboutController(IStringLocalizer&lt;AboutController&gt; localizer)
    {
        _localizer = localizer;
    }

    public IActionResult About()
    {
        ViewBag.Name = _localizer["Name"];
        Return View();
    }
</code></pre>

<p>So this is working fine, however I am curious how can I use <code>IStringLocalizer</code> in CustomAttribute from where I will be getting localized validation message.</p>

<p><strong>Model</strong></p>

<pre><code>public partial class LMS_User
{
    [RequiredFieldValidator("FirstNameRequired")]
    public string FirstName { get; set; }

    [RequiredFieldValidator("LastNameRequired")]
    public string LastName { get; set; }
}
</code></pre>

<p>from model I have passed the resource key to custom attribute where I will be retrieving the localized message.</p>

<p><strong>Custom Attribute</strong></p>

<pre><code> public class RequiredFieldValidator: ValidationAttribute , IClientModelValidator
    {
private readonly string resourcekey = string.Empty;        
public RequiredFieldValidator(string resourceID)
    {
        resourcekey = resourceID;
    }
}

public void AddValidation(ClientModelValidationContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        // Here I want to get localized message using SQL.
        var errorMessage = "This field is required field.";
        MergeAttribute(context.Attributes, "data-val", "true");
        MergeAttribute(context.Attributes, "data- val-Required",errorMessage);

}

private static bool MergeAttribute(IDictionary&lt;string, string&gt; attributes, string key, string value)
    {
        if (attributes.ContainsKey(key))
        {
            return false;
        }
        attributes.Add(key, value);
        return true;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        return ValidationResult.Success;
    }
</code></pre>

<p>So, how can I use <code>IStringLocalizer</code> in custom attribute ? I want to do this using SQL.</p>

<p>Any help on this appreaciated !</p>