2012-07-31 2 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 달 전에 우리는이 나쁜 솔루션과 함께 내 질문을 게시하기 때문에 대답하지 않기 때문에. 부끄러움을 묻는 이유는 무엇입니까?