0

en-US:

enter image description here

zh-CN:

enter image description here

Why are [Display(Name = "LoginUserName")] and other data annotations not working?

This is my localization config

enter image description here

What could be the fix for me?

1 Answer 1

0

You can try the following two ways:

1. Use the old way of localizing data annotations:

[Display(Name = nameof(MyResources.LoginUserName), 
         ResourceType = typeof(MyResources))]

2. DataAnnotation localization can be configured in the startup file(Put in Program.cs in .NET 6):

services.AddMvc()
    .AddViewLocalization(o=>o.ResourcesPath = "Resources")
    .AddDataAnnotationsLocalization(o=> {
        var type = typeof(ViewResource);
        var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
        var factory = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
        var localizer = factory.Create("ViewResource", assemblyName.Name);
        o.DataAnnotationLocalizerProvider = (t, f) => localizer;
    });

Here is a work demo, you can refer to it.

Hope this can help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.