簡體   English   中英

如何在Windows Azure網站(asp.net mvc4)上更改驗證語言?

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

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

但驗證消息仍然是英文,例如

輸入字段是必需的。

我的錯是什么?

我有同樣的問題。

我想Azure“網站”上沒有安裝“Microsoft .NET Framework 4.5語言包”。 似乎使用“Azure雲項目”是一個選項,因為您可以直接配置IIS。

另一個解決方案是等待Microsoft在Azure中包含語言包支持...

Personnaly,我決定覆蓋主要屬性。 大多數技巧都是[必需]和[正則表達式]。 見下文(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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM