model
[MetadataType(typeof(UserMetaData))]
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
}
public class UserMetaData
{
public int Id { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = "UserNameRequired")]
[LocalizedDisplayNameAttribute("UserName", NameResourceType = typeof(Resources.ModelValidation))]
public string UserName { get; set; }
}
view
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.UserName)
@Html.TextBoxFor(x => x.UserName)
@Html.ValidationMessageFor(x => x.UserName)
</div>
<div>
<input type="submit" value="Gönder" />
</div>
}
LocalizedDisplayNameAttribute
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
private PropertyInfo _nameProperty;
private Type _resourceType;
public LocalizedDisplayNameAttribute(string displayNameKey)
: base(displayNameKey)
{ }
public Type NameResourceType
{
get { return _resourceType; }
set
{
_resourceType = value;
//initialize nameProperty when type property is provided by setter
_nameProperty = _resourceType.GetProperty(base.DisplayName, BindingFlags.Static | BindingFlags.Public);
}
}
public override string DisplayName
{
get
{
//check if nameProperty is null and return original display name value
if (_nameProperty == null) { return base.DisplayName; }
return (string)_nameProperty.GetValue(_nameProperty.DeclaringType, null);
}
}
}
resource file
output
RequiredAttribute localization works, but LocalizedDisplayNameAttribute does not work. I cant find any solution to fix this.
Any suggestion, where is the missing?
To opt-in to localization, we need to modify our Startup file. We'll be registering a few services, configuring options, and registering middleware. All steps that are common-place for additions in an ASP.NET application. Starting in our ConfigureServices method, we need to make a call to AddLocalization .
"Data Annotation provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls." Why would I use Data Annotation? There are 3 main areas where you may use it; two of them are related to your data presentation to your end user and one is to design your database.
Localization is the process of customizing the globalized web application to a specific locale and culture. Various resources such as images and text for the specific locale are created. The resource file in localization is scoped to a particular page in an application.
DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.
You seem to be reinventing the wheels with this LocalizedDisplayNameAttribute
attribute. This functionality is already built into the framework, right into the [Display]
attribute that you could use directly:
public class UserMetaData
{
public int Id { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = nameof(Resources.ModelValidation.UserNameRequired)]
[Display(ResourceType = typeof(Resources.ModelValidation), Name = nameof(Resources.ModelValidation.UserName))]
public string UserName { get; set; }
}
As far as why your wheel reinvention doesn't work, well, can't say for sure. Why care when you could get rid of it anyways.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With