Mvc: Support localizing [DisplayAttribute] via IStringLocalizer

Created on 11 Nov 2015  路  12Comments  路  Source: aspnet/Mvc

Today we only localize validation based data annotations. We should expand this outside of the realm of validation (especially since you do a services.AddDataAnnotationsLocalization which says nothing about only adding validation based data annotations.

Having support like this would enable scenarios such as:

_Model_:

``` C#
public class User
{
[Display(Name = "Last Name")]
public string LastName { get; set; }
}

``` HTML
@model User
@addTagHelper *, Microsoft.AspNet.Mvc.TagHelpers

<label asp-for="LastName" /> <!-- Label would be localizable -->

This issue is for more than just [Display] but this data annotation is the one that showed itself during my sign off activities.

3 - Done enhancement feature-Localization needs design

Most helpful comment

@eilon let's do this one in 1.1 if it fits.

All 12 comments

I agree this is something we should investigate. I imagine we'd do the work in DataAnnotationsMetadataProvider to support this.

Can we extend DataAnnotationsModelMetadataProvider to make this happen, code snippet may look like this

public class LocalizedDataAnnotationsModelMetadataProvider: DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        var meta = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        meta.DisplayName = Localizer[propertyName];
    }
}

At this stage this is extremely unlikely to make it into 1.0.0, but we'll revisit it after we release.

I'm agree with you, this may be move to the backlog, because it need more investigation ..

there is no workaround or a temporary solution we can use until you support it?

Is there any progress on this issue? What's the schedule for this issue?

In the current state DisplayAttribute is rather useless for localized applications.

@ManuelHaas As I see it's still in backlog

@eilon let's do this one in 1.1 if it fits.

It's Ok if you want to do it in 1.1, but is there ANY current workaround for this one?

Most of my code was written when back in RC1 when localization wasn't even moving, but I just assumed it will work when it released, and now here I am, getting a bunch of not localized keys as results.

I'm like the 5th person asking for help in this one.

@ShikiGami we'll see if there's a workaround that works on 1.0, but I can't immediately think of any simple workaround.

Putting in the 1.1 milestone. @ryanbrandenburg , I marked this as needs-design because I'm not sure if this is straightforward ("just another attribute") or if this requires some extra plumbing in the system. So, have a look at this, and if it's not trivial, let's see if we can come up with some ideas and run it by @DamianEdwards / @dougbu (because it probably affects Model Metadata or something).

Keep waiting on this issiue.

https://github.com/dotnet/corefx/issues/11846

Was this page helpful?
0 / 5 - 0 ratings