2012-07-31 48 views

回答

-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个月前,我们来到这个不好解决。问这是否是一个耻辱,为什么我为此拒绝?