Skip to content

Commit b7e0b3e

Browse files
author
Alexander Smishlajev
committed
use self.client to translate messages; if that fails...
...take templating.translationService. fix translatable messages.
1 parent e8db584 commit b7e0b3e

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

roundup/cgi/form_parser.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import re, mimetypes
22

33
from roundup import hyperdb, date, password
4+
from roundup.cgi import templating
45
from roundup.cgi.exceptions import FormError
5-
from roundup.i18n import _
66

77
class FormParser:
88
# edit form variable handling (see unit tests)
@@ -26,14 +26,21 @@ class FormParser:
2626
)
2727
)
2828
)$'''
29-
29+
3030
def __init__(self, client):
3131
self.client = client
3232
self.db = client.db
3333
self.form = client.form
3434
self.classname = client.classname
3535
self.nodeid = client.nodeid
36-
36+
try:
37+
self._ = self.gettext = client.gettext
38+
self.ngettext = client.ngettext
39+
except AttributeError:
40+
_translator = templating.translationService
41+
self._ = self.gettext = _translator.gettext
42+
self.ngettext = _translator.ngettext
43+
3744
def parse(self, create=0, num_re=re.compile('^\d+$')):
3845
""" Item properties and their values are edited with html FORM
3946
variables and their values. You can:
@@ -79,7 +86,7 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
7986
@required
8087
The associated form value is a comma-separated list of
8188
property names that must be specified when the form is
82-
submitted for the edit operation to succeed.
89+
submitted for the edit operation to succeed.
8390
8491
When the <designator> is missing, the properties are
8592
for the current context item. When <designator> is
@@ -112,11 +119,11 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
112119
113120
For a Link('klass') property, the form value is a
114121
single key for 'klass', where the key field is
115-
specified in dbinit.py.
122+
specified in dbinit.py.
116123
117124
For a Multilink('klass') property, the form value is a
118125
comma-separated list of keys for 'klass', where the
119-
key field is specified in dbinit.py.
126+
key field is specified in dbinit.py.
120127
121128
Note that for simple-form-variables specifiying Link
122129
and Multilink properties, the linked-to class must
@@ -168,7 +175,7 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
168175
actual content, otherwise we remove them from all_props before
169176
returning.
170177
171-
The return from this method is a dict of
178+
The return from this method is a dict of
172179
(classname, id): properties
173180
... this dict _always_ has an entry for the current context,
174181
even if it's empty (ie. a submission for an existing issue that
@@ -273,15 +280,16 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
273280
for entry in self.extractFormList(form[key]):
274281
m = self.FV_DESIGNATOR.match(entry)
275282
if not m:
276-
raise FormError, \
277-
'link "%s" value "%s" not a designator'%(key, entry)
283+
raise FormError, self._('link "%(key)s" '
284+
'value "%(value)s" not a designator') % locals()
278285
value.append((m.group(1), m.group(2)))
279286

280287
# make sure the link property is valid
281288
if (not isinstance(propdef[propname], hyperdb.Multilink) and
282289
not isinstance(propdef[propname], hyperdb.Link)):
283-
raise FormError, '%s %s is not a link or '\
284-
'multilink property'%(cn, propname)
290+
raise FormError, self._('%(class)s %(property)s '
291+
'is not a link or multilink property') % {
292+
'class':cn, 'property':propname}
285293

286294
all_links.append((cn, nodeid, propname, value))
287295
continue
@@ -301,9 +309,10 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
301309
# does the property exist?
302310
if not propdef.has_key(propname):
303311
if mlaction != 'set':
304-
raise FormError, 'You have submitted a %s action for'\
305-
' the property "%s" which doesn\'t exist'%(mlaction,
306-
propname)
312+
raise FormError, self._('You have submitted a %(action)s '
313+
'action for the property "%(property)s" '
314+
'which doesn\'t exist') % {
315+
'action': mlaction, 'property':propname}
307316
# the form element is probably just something we don't care
308317
# about - ignore it
309318
continue
@@ -319,8 +328,8 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
319328
else:
320329
# multiple values are not OK
321330
if isinstance(value, type([])):
322-
raise FormError, 'You have submitted more than one value'\
323-
' for the %s property'%propname
331+
raise FormError, self._('You have submitted more than one '
332+
'value for the %s property') % propname
324333
# value might be a file upload...
325334
if not hasattr(value, 'filename') or value.filename is None:
326335
# nope, pull out the value and strip it
@@ -342,14 +351,14 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
342351
confirm = form[key]
343352
break
344353
else:
345-
raise FormError, 'Password and confirmation text do '\
346-
'not match'
354+
raise FormError, self._('Password and confirmation text '
355+
'do not match')
347356
if isinstance(confirm, type([])):
348-
raise FormError, 'You have submitted more than one value'\
349-
' for the %s property'%propname
357+
raise FormError, self._('You have submitted more than one '
358+
'value for the %s property') % propname
350359
if value != confirm.value:
351-
raise FormError, 'Password and confirmation text do '\
352-
'not match'
360+
raise FormError, self._('Password and confirmation text '
361+
'do not match')
353362
try:
354363
value = password.Password(value)
355364
except hyperdb.HyperdbValueError, msg:
@@ -383,8 +392,9 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
383392
try:
384393
existing.remove(entry)
385394
except ValueError:
386-
raise FormError, _('property "%(propname)s": '
387-
'"%(value)s" not currently in list')%{
395+
raise FormError, self._('property '
396+
'"%(propname)s": "%(value)s" '
397+
'not currently in list') % {
388398
'propname': propname, 'value': entry}
389399
else:
390400
# add - easy, just don't dupe
@@ -495,12 +505,11 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
495505
continue
496506

497507
# tell the user to entry the values required
498-
if len(required) > 1:
499-
p = 'properties'
500-
else:
501-
p = 'property'
502-
s.append('Required %s %s %s not supplied'%(thing[0], p,
503-
', '.join(required)))
508+
s.append(self.ngettext(
509+
'Required %(class)s property %(property)s not supplied',
510+
'Required %(class)s properties %(property)s not supplied',
511+
len(required)
512+
) % {'class': thing[0], 'property': ', '.join(required)})
504513
if s:
505514
raise FormError, '\n'.join(s)
506515

@@ -514,7 +523,7 @@ def parse(self, create=0, num_re=re.compile('^\d+$')):
514523
if not props.get('content', ''):
515524
del all_props[(cn, id)]
516525
elif props.has_key('content') and not props['content']:
517-
raise FormError, _('File is empty')
526+
raise FormError, self._('File is empty')
518527
return all_props, all_links
519528

520529
def extractFormList(self, value):

0 commit comments

Comments
 (0)