【问题标题】:How can I change the validation language on Windows Azure Web Site (asp.net mvc4)?如何更改 Windows Azure 网站 (asp.net mvc4) 上的验证语言?
【发布时间】:2013-10-10 08:36:23
【问题描述】:

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

但是验证信息还是英文,例如

输入字段是必需的。

我的错误是什么?

【问题讨论】:

  • 你弄明白了吗?
  • 没有。目前我使用自定义属性。

标签: asp.net-mvc asp.net-mvc-4 azure azure-web-app-service


【解决方案1】:

我也有同样的问题。

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

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

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

}

对于正则表达式:

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

还有

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

在 Global.asax 中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2018-04-24
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多