1

EntityFramework4.3で検証時に取得するデフォルトのErrorMessageを変更したいと思います。EF4.1用のLanguagePackが見つかりましたが、4.3用のLanguagePackはありませんか?

4

1 に答える 1

-1

遭遇したすべての検証メッセージを手動で翻訳することで、この問題を解決しました! いくつかの正規表現を使用して、最終的なエラー メッセージからメッセージ リソース文字列を抽出しました。

    private static Dictionary<string, string> _errorTranslation;

    private static string TranslateErrorMessage(string errorMessage)
    {
        if (_errorTranslation == null)
        {
            _errorTranslation = new Dictionary<string, string>();

            // MaxLength-Attribute
            _errorTranslation.Add(
                @"^The field (.*) must be a string or array type with a maximum length of (.*)\.$",
                @"Das Feld $1 hat eine maximale Länge von $2.");

            // MinLength-Attribute
            _errorTranslation.Add(
                @"^The field (.*) must be a string or array type with a minimum length of (.*)\.$",
                @"Das Feld $1 hat eine minimale Länge von $2.");

            // StringLength-Attribute
            _errorTranslation.Add(
                @"^The field (.*) must be a string with a maximum length of (.*)\.$",
                @"Das Feld $1 hat eine maximale Länge von $2.");

            // Range-Attribute
            _errorTranslation.Add(
                @"^The field (.*) must be between (.*) and (.*)\.$",
                @"Das Feld $1 muss zwichen $2 und $3 liegen.");

            // Required-Attribute
            _errorTranslation.Add(
                @"^The (.*) field is required\.$",
                @"Das Feld $1 wird zwingend benötigt.");
        }

        foreach (var pattern in _errorTranslation)
            if (Regex.IsMatch(errorMessage, pattern.Key))
                return Regex.Replace(errorMessage, pattern.Key, pattern.Value);

        return errorMessage;
    }

編集 5か月前に質問を投稿して以来、誰も答えなかったので、この悪い解決策を思いついた. なぜ私はこれに反対票を投じられたのですか?

于 2012-12-07T21:59:45.617 に答える