How to localize DataAnnotations from both SharedSo

2019-07-10 02:41发布

问题:

In .Net Core official documentation for localization, it is mentioned how to make DataAnnotations to be localized by IStringLocalizer from either a special resx file named by special .Net Core naming convention:

For example:
Resources/ViewModels.Account.RegisterViewModel.fr.resx Resources/ViewModels/Account/RegisterViewModel.fr.resx

or from SharedResource.resx file, from where ALL DataAnnotations will be localized:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddDataAnnotationsLocalization(options => {
            options.DataAnnotationLocalizerProvider = (type, factory) =>
                factory.Create(typeof(SharedResource));
        });
}

In the preceeding code, SharedResource is the class corresponding to the resx where your validation messages are stored. With this approach, DataAnnotations will only use SharedResource, rather than the resource for each class.

What I actually need to achieve is: defining which DataAnnotations will be localized from the SharedResource.resx file, and which one will be localized from ViewModel-specific.resx file. Is this somehow possible? In other words, I do not want to localize from different *.resx sources (where the fall-back value is the SharedResource.resx).

回答1:

This question is answered in another question bellow, thank's for @Tseng:

How to change ViewModel display name without using `DisplayNameAttribute`?