Ticket #3095: newfields_gettext.diff

File newfields_gettext.diff, 5.6 KB (added by Honza Král <Honza.Kral@…>, 9 years ago)

patch adding the gettext() calls

  • django/newforms/fields.py

     
    77import datetime
    88import re
    99import time
     10from django.utils.translation import gettext
    1011
    1112__all__ = (
    1213    'Field', 'CharField', 'IntegerField',
     
    5051        Raises ValidationError for any errors.
    5152        """
    5253        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.'))
    5455        return value
    5556
    5657class CharField(Field):
     
    6465        if value in EMPTY_VALUES: value = u''
    6566        value = smart_unicode(value)
    6667        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)
    6869        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)
    7071        return value
    7172
    7273class IntegerField(Field):
     
    8182        try:
    8283            return int(value)
    8384        except (ValueError, TypeError):
    84             raise ValidationError(u'Enter a whole number.')
     85            raise ValidationError(gettext(u'Enter a whole number.'))
    8586
    8687DEFAULT_DATE_INPUT_FORMATS = (
    8788    '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'
     
    113114                return datetime.date(*time.strptime(value, format)[:3])
    114115            except ValueError:
    115116                continue
    116         raise ValidationError(u'Enter a valid date.')
     117        raise ValidationError(gettext(u'Enter a valid date.'))
    117118
    118119DEFAULT_DATETIME_INPUT_FORMATS = (
    119120    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
     
    149150                return datetime.datetime(*time.strptime(value, format)[:6])
    150151            except ValueError:
    151152                continue
    152         raise ValidationError(u'Enter a valid date/time.')
     153        raise ValidationError(gettext(u'Enter a valid date/time.'))
    153154
    154155class RegexField(Field):
    155156    def __init__(self, regex, error_message=None, required=True, widget=None):
     
    162163        if isinstance(regex, basestring):
    163164            regex = re.compile(regex)
    164165        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.')
    166167
    167168    def clean(self, value):
    168169        """
     
    185186
    186187class EmailField(RegexField):
    187188    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)
    189190
    190191url_re = re.compile(
    191192    r'^https?://' # http:// or https://
     
    203204class URLField(RegexField):
    204205    def __init__(self, required=True, verify_exists=False, widget=None,
    205206            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)
    207208        self.verify_exists = verify_exists
    208209        self.user_agent = validator_user_agent
    209210
     
    223224                req = urllib2.Request(value, None, headers)
    224225                u = urllib2.urlopen(req)
    225226            except ValueError:
    226                 raise ValidationError(u'Enter a valid URL.')
     227                raise ValidationError(gettext(u'Enter a valid URL.'))
    227228            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.'))
    229230        return value
    230231
    231232class BooleanField(Field):
     
    254255            return value
    255256        valid_values = set([str(k) for k, v in self.choices])
    256257        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)
    258259        return value
    259260
    260261class MultipleChoiceField(ChoiceField):
     
    266267        Validates that the input is a list or tuple.
    267268        """
    268269        if self.required and not value:
    269             raise ValidationError(u'This field is required.')
     270            raise ValidationError(gettext(u'This field is required.'))
    270271        elif not self.required and not value:
    271272            return []
    272273        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.'))
    274275        new_value = []
    275276        for val in value:
    276277            val = smart_unicode(val)
     
    279280        valid_values = set([k for k, v in self.choices])
    280281        for val in new_value:
    281282            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)
    283284        return new_value
    284285
    285286class ComboField(Field):
Back to Top