如何从SharedSource.resx和ViewModel.resx本地化DataAnnotations [英] How to localize DataAnnotations from both SharedSource.resx AND ViewModel.resx

查看:138
本文介绍了如何从SharedSource.resx和ViewModel.resx本地化DataAnnotations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net核心本地化官方文档中,提到了如何使DataAnnotationsIStringLocalizer本地化,该方法来自以特殊.Net Core命名约定命名的特殊resx文件:

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:

例如:
资源/ViewModels.Account.RegisterViewModel.fr.resx 资源/ViewModels/帐户/RegisterViewModel.fr.resx

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

SharedResource.resx文件中的 ALL 数据注释将在其中进行本地化:

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));
        });
}

在前面的代码中,SharedResource是与存储验证消息的resx对应的类.通过这种方法,DataAnnotations将仅使用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.

我实际上需要实现的是:定义将从SharedResource.resx文件本地化的DataAnnotations,从ViewModel-specific.resx文件本地化的. 换句话说,我不想从不同的*.resx来源(回退值为SharedResource.resx)进行本地化.

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).

推荐答案

此问题在另一个问题中回答,谢谢@Tseng:

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

如何在不使用ViewModel的情况下更改ViewModel显示名称使用"DisplayNameAttribute"?

这篇关于如何从SharedSource.resx和ViewModel.resx本地化DataAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆