Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aspnet-hello opened this issue Jan 2, 2018 · 1 comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates question

Comments

@aspnet-hello
Copy link

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

@aspnet-hello
Copy link
Author

From @pauldotknopf on Friday, April 28, 2017 9:35:36 AM

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.

@Eilon Eilon added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed repo:Localization labels Nov 7, 2018
ryanbrandenburg pushed a commit that referenced this issue Nov 27, 2018
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates question
Projects
None yet
Development

No branches or pull requests

3 participants