<globalization culture="de-DE" uiCulture="de-DE" />
既にWeb.config に追加しています。テストビューに追加@Thread.CurrentThread.CurrentCulture
すると、de-DE が表示されます。それで、すべて問題ないようです。
ただし、検証メッセージは英語のままです。
入力フィールドは必須です。
私の間違いは何ですか?
<globalization culture="de-DE" uiCulture="de-DE" />
既にWeb.config に追加しています。テストビューに追加@Thread.CurrentThread.CurrentCulture
すると、de-DE が表示されます。それで、すべて問題ないようです。
ただし、検証メッセージは英語のままです。
入力フィールドは必須です。
私の間違いは何ですか?
私は同じ問題を抱えています。
Azureの「Webサイト」には「Microsoft .NET Framework 4.5 Language Pack」がインストールされていないと思います。IISを直接構成できるため、「Azureクラウドプロジェクト」の使用がオプションのようです。
もう 1 つの解決策は、Microsoft が Azure に言語パックのサポートを含めるのを待つことです...
個人的には、主な属性をオーバーライドすることにしました。最もトリッキーなのは [Required] と [Regex] です。以下を参照してください(gettextを使用しているため、Locは私自身のローカリゼーション関数です)
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));