简体   繁体   English

如何在Windows Azure网站(asp.net mvc4)上更改验证语言?

[英]How can I change the validation language on Windows Azure Web Site (asp.net mvc4)?

I've already added <globalization culture="de-DE" uiCulture="de-DE" /> to my Web.config. 我已经在我的Web.config中添加了<globalization culture="de-DE" uiCulture="de-DE" /> I added @Thread.CurrentThread.CurrentCulture to my testview and it shows de-DE. 我将@Thread.CurrentThread.CurrentCulture添加到我的testview中,它显示了de-DE。 So, all seems fine. 所以,一切似乎都很好。

But the validation message is still in English, eg 但验证消息仍然是英文,例如

The Input field is required. 输入字段是必需的。

What is my mistake? 我的错是什么?

I am having the same problem. 我有同样的问题。

I suppose that "Microsoft .NET Framework 4.5 Language Pack" is not installed on Azure "Web sites". 我想Azure“网站”上没有安装“Microsoft .NET Framework 4.5语言包”。 It seems using an "Azure cloud project" is an option as you can then configure IIS directly. 似乎使用“Azure云项目”是一个选项,因为您可以直接配置IIS。

Another solution is to wait for Microsoft to include Language Pack support in Azure... 另一个解决方案是等待Microsoft在Azure中包含语言包支持...

Personnaly, I have decided to override the main attributes. Personnaly,我决定覆盖主要属性。 Most trickiests are [Required] and [Regex]. 大多数技巧都是[必需]和[正则表达式]。 See below (Loc is my own Localization function as I am using gettext) 见下文(Loc是我自己的本地化函数,因为我正在使用gettext)

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;

namespace Ic.DataAnnotations
{
    public class RequiredLoc : RequiredAttribute, IClientValidatable
    {

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,
            ControllerContext context)
        {
            yield return new ModelClientValidationRule
            {
                ErrorMessage = FormatErrorMessage(metadata.DisplayName),
                ValidationType = "required"
            };
        }

        public override string FormatErrorMessage(string message)
        {
            base.FormatErrorMessage(message);
            return Localizer.Loc("Le champs '%1' est requis.", message);
        }
    }

}

For regular expressions: 对于正则表达式:

using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace Ic.DataAnnotations
{
    public class RegularExpressionLoc : RegularExpressionAttribute
    {
        private readonly string _errorMessage;

        public RegularExpressionLoc(string errorMessage, string pattern)
            : base(pattern)
        {
            _errorMessage = errorMessage;
        }

        public override string FormatErrorMessage(string input)
        {
            return Localizer.Loc(_errorMessage);
        }
    }
}

And

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Ic.DataAnnotations
{
    public class RegularExpressionValidator : DataAnnotationsModelValidator<RegularExpressionAttribute>
    {
        private readonly string _message;
        private readonly string _pattern;

        public RegularExpressionValidator(ModelMetadata metadata, ControllerContext context, RegularExpressionAttribute attribute)
            : base(metadata, context, attribute)
        {
            _pattern = attribute.Pattern;
            _message = attribute.FormatErrorMessage("");
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = _message,
                ValidationType = "regex"
            };

            rule.ValidationParameters.Add("pattern", _pattern);

            return new[] { rule };
        }
    }
}

And in Global.asax 并在Global.asax

  DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RegularExpressionLoc), typeof(RegularExpressionValidator));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM