惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - Jans

Flutter运行时闪退 利用 Python 爬虫来对文本进行批量化翻译 Thinkpad T460声音问题 Error 56: The Cisco Systems, Inc. VPN Service has not been started(Cisco VPN在Vista/Win 7下出现Error 56的解决办法) - Jans Weblogic中配置Active Directory Authentication Provider weblogic 10.3.5重置密码 在基于WCF开发的Web Service导出WSDL定义问题及自定义wsdl:port 名称 JNI:在线程或信号处理函数中访问自定义类 (转)Java实现Web Service过程中处理SOAP Header的问题 Geek to Live: Set up your personal Wikipedia - Jans (转)GitHub上整理的一些工具,求补充 - (转)老衣的开发工具和类库集之2014版 GCC的内存边界对齐 如何删除Weblogic域 电蚊拍选购参考 Localizing WPF with .resx files C#操作串口总结 小众的分布式版本管理工具Code Co-op 关于效率长尾现象
ASP.NET MVC的国际化问题
Jans · 2013-04-25 · via 博客园 - Jans

在博客园里有这篇帖子讲的已经很全面了,但是我认为还不够优雅,整个看起来好混乱,像国外这位老兄的帖子处理的就很整洁,简单记录如下:

通常的国际化处理方法

public class User
{
    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
    [LocalizedDisplayNameAttribute("User_Id", NameResourceType=typeof(ModelTranslations))]
    public int Id { get; set; }
 
    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
    [StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))]
    [LocalizedDisplayNameAttribute("User_FirstName", NameResourceType=typeof(ModelTranslations))]
    public string FirstName { get; set; }
 
    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
    [StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))]
    [LocalizedDisplayNameAttribute("User_LastName", NameResourceType=typeof(ModelTranslations))]
    public string LastName { get; set; }
}

可以看出来很混乱。

public class User
{
    [Required]
    [LocalizedDisplayNameAttribute("User_Id")]
    public int Id { get; set; }
 
    [Required]
    [StringLength(40)]
    [LocalizedDisplayNameAttribute("User_FirstName")]
    public string FirstName { get; set; }
 
    [Required]
    [StringLength(40)]
    [LocalizedDisplayNameAttribute("User_LastName")]
    public string LastName { get; set; }
}

是不是整洁多了,但是这个整洁的代价就是要背后要写代码的

public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    private string _displayName;
 
    public RequiredAttribute()
    {
        ErrorMessageResourceName = "Validation_Required";
    }
 
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        _displayName = validationContext.DisplayName;
        return base.IsValid(value, validationContext);
    }
 
    public override string FormatErrorMessage(string name)
    {
        var msg = LanguageService.Instance.Translate(ErrorMessageResourceName);
        return string.Format(msg, _displayName);
    }
}

这个是验证属性的实现代码。

ublic class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    private PropertyInfo _nameProperty;
    private Type _resourceType;
 
    public LocalizedDisplayNameAttribute(string className, string propertyName)
        : base(className + (propertyName == null ? "_Class" : ("_" + propertyName)))
    {
 
    }
 
    public override string DisplayName
    {
        get
        {
            return LanguageService.Instance.Translate(base.DisplayName) ?? "**" + base.DisplayName + "**";
        }
    }
}

这是实现DisplayName属性的实现代码

public class LanguageService
{
    private static LanguageService _instance = new LanguageService();
    private List<ResourceManager> _resourceManagers = new List<ResourceManager>();
 
    private LanguageService()
    {
    }
 
    public static LanguageService Instance { get { return _instance; } }
 
    public void Add(ResourceManager mgr)
    {
        _resourceManagers.Add(mgr);
    }
 
    public string Translate(string key)
    {
        foreach (var item in _resourceManagers)
        {
            var value = item.GetString(key);
            if (value != null)
                return value;
        }
 
        return null;
    }
}

   这是上述类依赖的管理资源的代码。

但上述的解决办法对于客户端验证还是有问题,以下是一个简单的实现

public class RequiredAttributeAdapter : DataAnnotationsModelValidator<RequiredAttribute>
{
    public RequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
        : base(metadata, context, attribute)
    {
    }
 
    public static void SelfRegister()
    {
        DataAnnotationsModelValidatorProvider
            .RegisterAdapter(
                typeof (RequiredAttribute),
                typeof (RequiredAttributeAdapter));
    }
 
    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        return new[] { new ModelClientValidationRequiredRule(ErrorMessage) };
    }
}

然后在Global.asax里加一句

RequiredAttributeAdapter.SelfRegister();

完。