Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

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

Closed
pauldotknopf opened this issue Apr 28, 2017 · 2 comments

Comments

@pauldotknopf
Copy link

pauldotknopf commented Apr 28, 2017

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?

@pauldotknopf
Copy link
Author

pauldotknopf commented Apr 28, 2017

I figured it out, after looking at the source.

I implemented my own IStringLocalizerFactory, and added this.

services.Configure<MvcDataAnnotationsLocalizationOptions>(options =>
            {
                options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(null);
            });

I tried to use the AddDataAnnotationsLocalization extension method when using AddMvc, but for some reason, the IStringLocalizerFactory being passed to the DataAnnotationLocalizerProvider callback was always the default resource-based implementation, regardless of my own IStringLocalizerFactory.

@aspnet-hello
Copy link

This issue was moved to dotnet/aspnetcore#2639

@aspnet aspnet locked and limited conversation to collaborators Jan 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants