Ticket #3095: newfields_gettext.diff
File newfields_gettext.diff, 5.6 KB (added by , 18 years ago) |
---|
-
django/newforms/fields.py
7 7 import datetime 8 8 import re 9 9 import time 10 from django.utils.translation import gettext 10 11 11 12 __all__ = ( 12 13 'Field', 'CharField', 'IntegerField', … … 50 51 Raises ValidationError for any errors. 51 52 """ 52 53 if self.required and value in EMPTY_VALUES: 53 raise ValidationError( u'This field is required.')54 raise ValidationError(gettext(u'This field is required.')) 54 55 return value 55 56 56 57 class CharField(Field): … … 64 65 if value in EMPTY_VALUES: value = u'' 65 66 value = smart_unicode(value) 66 67 if self.max_length is not None and len(value) > self.max_length: 67 raise ValidationError( u'Ensure this value has at most %d characters.'% self.max_length)68 raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length) 68 69 if self.min_length is not None and len(value) < self.min_length: 69 raise ValidationError( u'Ensure this value has at least %d characters.'% self.min_length)70 raise ValidationError(gettext(u'Ensure this value has at least %d characters.') % self.min_length) 70 71 return value 71 72 72 73 class IntegerField(Field): … … 81 82 try: 82 83 return int(value) 83 84 except (ValueError, TypeError): 84 raise ValidationError( u'Enter a whole number.')85 raise ValidationError(gettext(u'Enter a whole number.')) 85 86 86 87 DEFAULT_DATE_INPUT_FORMATS = ( 87 88 '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06' … … 113 114 return datetime.date(*time.strptime(value, format)[:3]) 114 115 except ValueError: 115 116 continue 116 raise ValidationError( u'Enter a valid date.')117 raise ValidationError(gettext(u'Enter a valid date.')) 117 118 118 119 DEFAULT_DATETIME_INPUT_FORMATS = ( 119 120 '%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' … … 149 150 return datetime.datetime(*time.strptime(value, format)[:6]) 150 151 except ValueError: 151 152 continue 152 raise ValidationError( u'Enter a valid date/time.')153 raise ValidationError(gettext(u'Enter a valid date/time.')) 153 154 154 155 class RegexField(Field): 155 156 def __init__(self, regex, error_message=None, required=True, widget=None): … … 162 163 if isinstance(regex, basestring): 163 164 regex = re.compile(regex) 164 165 self.regex = regex 165 self.error_message = error_message or u'Enter a valid value.'166 self.error_message = error_message or gettext(u'Enter a valid value.') 166 167 167 168 def clean(self, value): 168 169 """ … … 185 186 186 187 class EmailField(RegexField): 187 188 def __init__(self, required=True, widget=None): 188 RegexField.__init__(self, email_re, u'Enter a valid e-mail address.', required, widget)189 RegexField.__init__(self, email_re, gettext(u'Enter a valid e-mail address.'), required, widget) 189 190 190 191 url_re = re.compile( 191 192 r'^https?://' # http:// or https:// … … 203 204 class URLField(RegexField): 204 205 def __init__(self, required=True, verify_exists=False, widget=None, 205 206 validator_user_agent=URL_VALIDATOR_USER_AGENT): 206 RegexField.__init__(self, url_re, u'Enter a valid URL.', required, widget)207 RegexField.__init__(self, url_re, gettext(u'Enter a valid URL.'), required, widget) 207 208 self.verify_exists = verify_exists 208 209 self.user_agent = validator_user_agent 209 210 … … 223 224 req = urllib2.Request(value, None, headers) 224 225 u = urllib2.urlopen(req) 225 226 except ValueError: 226 raise ValidationError( u'Enter a valid URL.')227 raise ValidationError(gettext(u'Enter a valid URL.')) 227 228 except: # urllib2.URLError, httplib.InvalidURL, etc. 228 raise ValidationError( u'This URL appears to be a broken link.')229 raise ValidationError(gettext(u'This URL appears to be a broken link.')) 229 230 return value 230 231 231 232 class BooleanField(Field): … … 254 255 return value 255 256 valid_values = set([str(k) for k, v in self.choices]) 256 257 if value not in valid_values: 257 raise ValidationError( u'Select a valid choice. %s is not one of the available choices.'% value)258 raise ValidationError(gettext(u'Select a valid choice. %s is not one of the available choices.') % value) 258 259 return value 259 260 260 261 class MultipleChoiceField(ChoiceField): … … 266 267 Validates that the input is a list or tuple. 267 268 """ 268 269 if self.required and not value: 269 raise ValidationError( u'This field is required.')270 raise ValidationError(gettext(u'This field is required.')) 270 271 elif not self.required and not value: 271 272 return [] 272 273 if not isinstance(value, (list, tuple)): 273 raise ValidationError( u'Enter a list of values.')274 raise ValidationError(gettext(u'Enter a list of values.')) 274 275 new_value = [] 275 276 for val in value: 276 277 val = smart_unicode(val) … … 279 280 valid_values = set([k for k, v in self.choices]) 280 281 for val in new_value: 281 282 if val not in valid_values: 282 raise ValidationError( u'Select a valid choice. %s is not one of the available choices.'% val)283 raise ValidationError(gettext(u'Select a valid choice. %s is not one of the available choices.') % val) 283 284 return new_value 284 285 285 286 class ComboField(Field):