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

Har*_*rry 6 asp.net-mvc azure asp.net-mvc-4 azure-web-sites

我已经添加<globalization culture="de-DE" uiCulture="de-DE" />到我的Web.config中了.我添加@Thread.CurrentThread.CurrentCulture到我的testview中,它显示了de-DE.所以,一切似乎都很好.

但验证消息仍然是英文,例如

输入字段是必需的.

我的错是什么?

小智 0

我有同样的问题。

我认为 Azure“网站”上未安装“Microsoft .NET Framework 4.5 语言包”。似乎使用“Azure 云项目”是一个选项,因为您可以直接配置 IIS。

另一个解决方案是等待 Microsoft 在 Azure 中包含语言包支持......

就我个人而言,我决定覆盖主要属性。最棘手的是[必需]和[正则表达式]。见下文(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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

对于正则表达式:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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 };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Global.asax 中

  DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RegularExpressionLoc), typeof(RegularExpressionValidator));
Run Code Online (Sandbox Code Playgroud)