I have a multilanguage site running on ASP.NET Core 6 MVC.
The data annotation should be based on user language; I can make the site bilingual using sharedResource
class.
The issue is how to make the model data annotation error bilingual; currently, I only got the data annotation ErrorMessage
.
Program.cs
builder.Services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
//.AddDataAnnotationsLocalization();// <--- for ERROR MSG -----
.AddDataAnnotationsLocalization(
options => {
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(DataAnnotationResource));
});// <---------- For ERROR MSG -----
FactoryData Model
public class FactoryData
{
[Required(ErrorMessage = "General.RequiresMessageOOO")]
public string NameInAr { get; set; }
[Required(ErrorMessage = "General.RequiresMessageOOO")]
[MaxLength(2, ErrorMessage = "General.MaxlengthExceededOOO")]
public string NameInEn { get; set; }
[Required]
[Range(1,3)]
public string Age { get; set; }
}
This is the localizationResource
folder:
The output of this current code