I Add a Resource File to my projects and I write a Service :
using Microsoft.Extensions.Localization;
using System.Reflection;
namespace NzShop.Services
{
public class ComponentsResourceService
{
private readonly IStringLocalizer localizer;
public ComponentsResourceService(IStringLocalizerFactory factory)
{
var assemblyName = new AssemblyName(typeof(ComponentsResource).GetTypeInfo().Assembly.FullName!);
localizer = factory.Create(nameof(ComponentsResource), assemblyName.Name!);
}
public string Get(string key)
{
return localizer[key];
}
public class ComponentsResource { }
}
}
In Program.cs add:
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("fa"),
new CultureInfo("tr"),
new CultureInfo("en-US")
};
options.DefaultRequestCulture = new RequestCulture("en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
builder.Services.AddMvc().AddViewLocalization()
.AddDataAnnotationsLocalization();
builder.Services.AddSingleton<ComponentsResourceService>();
How Can control Required Field from my ComponentsResource.resx file:
[Required(ErrorMessage = ?????)]
public string Familly { get; set; }