更改Entity Framework 4.3的默认ErrorMessage

时间:2012-07-31 15:18:03

标签: c# entity-framework-4.3

我想更改Entity Framework 4.3中验证的默认ErrorMessage。我们为EF4.1找到了LanguagePacks但是没有4.3的LanguagePacks可用吗?

1 个答案:

答案 0 :(得分:-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个月前发布问题以来没有人回答我们带来了这个糟糕的解决方案。这是一种耻辱,我为什么要为此投票?