Skip to content

Question, recommend approach for localization of DisplayAttribute, without .resx files. #2639

@aspnet-hello

Description

@aspnet-hello

From @pauldotknopf on Friday, April 28, 2017 7:45:21 AM

I am using my own localization engine (using PO/POT/MO files). I am not using resx files or satellite culture assemblies.

How do I localize the DisplayAttribute?

[Display(Name = "Test")]

I noticed the ResourceType parameter, and I get it. It uses resource classes. However, I was curious, maybe I could change how this works, via DI or some option/configuration stuff. I set the ResourceType to a dummy class, to see what the callstack was.

InvalidOperationException: Cannot retrieve property 'Name' because localization failed. Type 'MedXStorage.Web.Areas.Public.Features.Account.Models.RegisterViewModel+TestResource' is not public or does not contain a public static string property with the name 'Confirm password'.
System.ComponentModel.DataAnnotations.LocalizableString+<>c__DisplayClass12_0.<GetLocalizableValue>b__1()
System.ComponentModel.DataAnnotations.LocalizableString.GetLocalizableValue()
Microsoft.AspNetCore.Mvc.DataAnnotations.Internal.DataAnnotationsMetadataProvider.CreateDisplayMetadata(DisplayMetadataProviderContext context)
Microsoft.AspNetCore.Mvc.Internal.DefaultCompositeMetadataDetailsProvider.CreateDisplayMetadata(DisplayMetadataProviderContext context)
Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_DisplayMetadata()
Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_Order()
System.Linq.EnumerableSorter.ComputeKeys(TElement[] elements, int count)
System.Linq.EnumerableSorter.ComputeMap(TElement[] elements, int count)
System.Linq.EnumerableSorter.Sort(TElement[] elements, int count)
System.Linq.OrderedEnumerable.ToList()
System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)
Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_Properties()
Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ExpressionMetadataProvider.FromLambdaExpression<TModel, TResult>(Expression<Func<TModel, TResult>> expression, ViewDataDictionary<TModel> viewData, IModelMetadataProvider metadataProvider)
Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpressionProvider.CreateModelExpression<TModel, TValue>(ViewDataDictionary<TModel> viewData, Expression<Func<TModel, TValue>> expression)

It looks like I could possible create some kind of meta-data provider that will look at my DisplayAttributes instances, and attempt to localize them. Is this correct? Is this the recommend way of doing custom localization of Display attributes?

Copied from original issue: aspnet/Localization#362

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions