2013-10-03 52 views
6

我已經將<globalization culture="de-DE" uiCulture="de-DE" />添加到我的Web.config中。我在測試視圖中添加了@Thread.CurrentThread.CurrentCulture,它顯示了de-DE。所以,一切似乎都很好。如何更改Windows Azure網站(asp.net mvc4)上的驗證語言?

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

輸入字段是必需的。

我的錯誤是什麼?

+0

你知道嗎? – bob

+1

不。目前我使用自定義屬性。 – Harry

回答

0

我遇到同樣的問題。

我想Azure的「網站」上沒有安裝「Microsoft .NET Framework 4.5語言包」。似乎使用「Azure雲項目」是一種選擇,因爲您可以直接配置IIS。

另一個解決方案是等待微軟包括在Azure的語言包,支持...

Personnaly,我已決定重寫的主要屬性。大多數trickiests是[必需的]和[正則表達式]。見下文(祿是我使用的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));