Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3095 -- Added translation strings to newforms. Thanks for the …

…patch, Honza Král

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4166 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e6d19257bac486583aec5ba502eccd1f8ae8d9f5 1 parent c7345f4
Adrian Holovaty authored December 06, 2006

Showing 1 changed file with 16 additions and 15 deletions. Show diff stats Hide diff stats

  1. 31  django/newforms/fields.py
31  django/newforms/fields.py
@@ -2,6 +2,7 @@
2 2
 Field classes
3 3
 """
4 4
 
  5
+from django.utils.translation import gettext
5 6
 from util import ValidationError, smart_unicode
6 7
 from widgets import TextInput, CheckboxInput, Select, SelectMultiple
7 8
 import datetime
@@ -50,7 +51,7 @@ def clean(self, value):
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,9 +65,9 @@ def clean(self, value):
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,7 +82,7 @@ def clean(self, value):
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,7 +114,7 @@ def clean(self, value):
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,7 +150,7 @@ def clean(self, value):
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,7 +163,7 @@ 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,7 +186,7 @@ def clean(self, value):
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,7 +204,7 @@ def __init__(self, required=True, widget=None):
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,9 +224,9 @@ def clean(self, value):
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,7 +255,7 @@ def clean(self, value):
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,11 +267,11 @@ def clean(self, value):
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,7 +280,7 @@ def clean(self, value):
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):

0 notes on commit e6d1925

Please sign in to comment.
Something went wrong with that request. Please try again.