Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization of data annotations using IStringLocalizer

I trying to implement localization in .net core 1.0 application using IStringLocalizer. I am able to do the localization for the view for which I have written something like this

    private readonly IStringLocalizer<AboutController> _localizer;

    public AboutController(IStringLocalizer<AboutController> localizer)
    {
        _localizer = localizer;
    }

    public IActionResult About()
    {
        ViewBag.Name = _localizer["Name"];
        Return View();
    }

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

Model

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

    [RequiredFieldValidator("LastNameRequired")]
    public string LastName { get; set; }
}

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

Custom Attribute

 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<string, string> 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;
    }

So, how can I use IStringLocalizer in custom attribute ? I want to do this using SQL.

Any help on this appreaciated !

like image 420
XamDev Avatar asked Dec 08 '25 06:12

XamDev


1 Answers

I like to implement localization as a service.

public RequiredFieldValidator(IStringLocalizer localizationService, string resourceID)
    {
        resourcekey = resourceID;
        localization = localizationService;
    }

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

        // Here I want to get localized message using SQL.
        var errorMessage = lozalization["requiredFieldMessage"];
        MergeAttribute(context.Attributes, "data-val", "true");
        MergeAttribute(context.Attributes, "data- val-Required",errorMessage);

}

you can choose implement the interface using resource strings, accessing to database to get the translations,... Here i'm implementing one method accessing to resource strings, assuming that the resources are in the same project.

public class LocalizationService : IStringLocalizer {

  public LocalizedString this[string name] {
    return new LocalizedString(name, Properties.Resources.GetString(name));
  }

//implement the rest of methods of IStringLocalizer 

}
like image 87
mnieto Avatar answered Dec 09 '25 19:12

mnieto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!