Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataannotations localization

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

enter image description here

output

enter image description here

RequiredAttribute localization works, but LocalizedDisplayNameAttribute does not work. I cant find any solution to fix this.

Any suggestion, where is the missing?

like image 201
AliRıza Adıyahşi Avatar asked May 30 '13 13:05

AliRıza Adıyahşi


People also ask

How ASP Net applications are localized?

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 .

What are DataAnnotations in net core?

"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.

What is localization in asp net?

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.

What is DataAnnotations MVC?

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.


1 Answers

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.

like image 183
Darin Dimitrov Avatar answered Oct 17 '22 05:10

Darin Dimitrov