Proposal about easier validators i18n

1 view
Skip to first unread message

Iceberg

unread,
Sep 5, 2009, 3:29:59 AM9/5/09
to web2py-users
Hi Massimo,

Currently the error_message of all validators are a phrase in English,
such as 'value already in database!' etc. When I gonna do some i18n
for it, I have to do:

db.Field('foo',
requires=IS_NOT_IN_DB(
db,"table.field",
error_message=T('value already in database!')))

db.Field('bar',
requires=IS_NOT_IN_DB(
db,"table.field2",
error_message=T('value already in database!'))) # breaks DRY
rule!

So I suggest we either:
(1) let the default error_message be i18n-ready:
from ... import T
class IS_NOT_IN_DB(...):
def __init__(self,
...
error_message=T('value already in database!'),
...):

or if somehow you don't like validators.py depends on T(), then you
can:
(2) let the default error_message be easier customizable:
class IS_NOT_IN_DB(...):
error_message = 'value already in database' # class wide
def __init__(self,
...
error_message=None,
):
if not error_message:
self.error_message = error_message

so in app's model file we can:
IS_NOT_IN_DB.error_message = T('value already in database!') # class
wide
...
db.Field('foo',
requires=IS_NOT_IN_DB(db,"table.field"))
db.Field('bar',
requires=IS_NOT_IN_DB(db,"table.field2"))


Same proposal applies to all other validators.

Regards,
Iceberg

Fran

unread,
Sep 5, 2009, 5:21:48 AM9/5/09
to web2py-users
On Sep 5, 8:29 am, Iceberg <iceb...@21cn.com> wrote:
> Currently the error_message of all validators are a phrase in English

+1 for getting this fixed in a DRY manner :)

> (1) let the default error_message be i18n-ready:
> or if somehow you don't like validators.py depends on T(), then you
> can:

tools.py does this by using the Messages() class for messages.
We should follow the same pattern if possible:

class IS_NOT_IN_DB(...):
def __init__(..., environment):
self.messages = Messages(None)
self.messages.error = 'value already in database!'
self.messages['T'] = self.environment.T

I suspect this isn't possible, though, since gluon modules are
executed rather than being imported?

> (2) let the default error_message be easier customizable:
> so in app's model file we can:
>   IS_NOT_IN_DB.error_message = T('value already in database!') # class

This would a reasomable backwards-compatible alternative, especially
if a file was added to the scaffolding apps's models folder which had
all these strings in (& was maintained as/when new validators are
added).
(This file can easily be copied into other existing apps)

On syntax, I think we should maintain the one which comes with tools,
so:
IS_NOT_IN_DB.messages.error = T('value already in database!')

F

mdipierro

unread,
Sep 5, 2009, 8:27:49 AM9/5/09
to web2py-users
I agree with Fran.

Iceberg

unread,
Sep 5, 2009, 9:21:20 AM9/5/09
to web2py-users
Does this mean there is no objection, and the i18n-able validators
will soon be available in trunk? :-)

mdipierro

unread,
Sep 5, 2009, 10:23:42 AM9/5/09
to web2py-users
I did not say that but let's all think more how this could be done.
Reply all
Reply to author
Forward
0 new messages