Код: Выделить всё
using Data.Resources.Localization.Shared.ValidationMessages;
using Microsoft.Extensions.Localization;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
namespace Data.Attributes;
public sealed class LocalizedRegularExpressionAttribute : RegularExpressionAttribute
{
public LocalizedRegularExpressionAttribute([StringSyntax("Regex")] string pattern, string errorMessageKey) : base(pattern)
{
ErrorMessageKey = errorMessageKey;
}
public string ErrorMessageKey { get; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var localizer = (IStringLocalizer)validationContext.GetService(typeof(IStringLocalizer));
ErrorMessage = localizer[ErrorMessageKey].Value;
return base.IsValid(value, validationContext);
}
}
Код: Выделить всё
var localizer = (IStringLocalizer)validationContext.GetService(typeof(IStringLocalizer));
У меня есть эта строка в program.cs в обоих проектах:
Код: Выделить всё
builder.Services.AddLocalization();
Подробнее здесь: https://stackoverflow.com/questions/790 ... lazor-wasm