On Dec 31, 2:34 pm, Jonathan Lundell <jlund...@pobox.com> wrote:
> On Dec 31, 2010, at 11:20 AM, Martin Weissenboeck wrote:
>
> > No, I did not not want to translate these sentences manually.
> > The error_message=T(..) does not work, because there are some if-statements
> > inside the class IS_INT_IN_RANGE (and in some other classes).
>
> > One solution could be a list of error messages for these classes.
>
> You don't need a list, because for any given instance, only one of the error
> messages is used, depending on which combination of min & max you set.
>
True.
>
> > My proposal uses T as an addition parameter of the __init__ methode of
> > class IS_INT_IN_RANGE (and all other classes) in validators.py and
> > therefore there is not compatiblity problem. I can use everything as before
> > and the translation method (T-search) works with every "internal"
> > error_message.
>
> I think the T-search would have to change, since it doesn't look in gluon.
> Not a big change.
>
>
>
>
>
> > This usage of T has another advantage:
>
> > Currenty error_messages are defined twice:
> > 1st inside validators.py, e.g.:
> > def __init__(self, expression, error_message='invalid expression'):
> > ...
> > self.error_message = error_message
>
> > 2nd on using the class, e.g.
> > a=IS_MATCH(...,error_message=T('invalid expression')
> > I think, this is against the rule "Don't repeat yourself"
>
> > With an additional T-parameter these lines could be written as follows:
> > 1st inside validators.py, e.g.:
> > def __init__(self, expression, error_message='invalid expression', T=lambda
> > x:x):
> > ...
> > self.error_message = T(error_message)
>
> > 2nd on using the class, e.g.
> > a=IS_MATCH(...,T=T) # if you like the default message
> > Now the default error message appears only once, but could be changed by
> > the user.
> > But I don't know whether this will slow down web2py.
>
> The ideal solution would be one that allowed using T() normally in gluon (at
> least in code that's invoked from an application); that avoids the T=T
> requirement.
>
> My conjecture is when Python compiles the gluon code, it binds T references
> (to what? that's the hole in my theory) at compile time, and thus ignores the
> presence of T in globals. Making T an argument forces Python to defer binding.
>
The funny part is that T is in environment (!!!?) so
exec ccode in environment
should work, I don't see why it does not.
>
> > 2010/12/31 Jonathan Lundell <jlund...@pobox.com>
> > On Dec 31, 2010, at 6:18 AM, Martin Weissenboeck wrote:
> >> I want to translate my web page to German. The T()"operator" is very fine,
> >> but I could not find any way to translate message like
> >> "enter an integer less than or equal to %(max)g"
> >> in class IS_INT_IN_RANGE.
>
> >> I think it is very unprofessional to mix English and German words and I
> >> have tried to find a solution.
>
> > Am I correct that you had to make the translation entries manually (because
> > the T() search doesn't look in gluon)?
>
> > I assume that the current logic was written with the intention of passing
> > in error_message=T(something); does that work too?
>
> > I wonder whether there isn't a general solution that would let us use T()
> > in gluon code that's invoked by applications.
>
> >> I have changed the following lines (file validators.py, class
> >> IS_INT_IN_RANGE)
>
> >> def __init__(
> >> self,
> >> minimum=None,
> >> maximum=None,
> >> error_message=None,
> >> T=lambda x:x,
> >> ):
> >> self.minimum = self.maximum = None
> >> if minimum is None:
> >> if maximum is None:
> >> if error_message is None:
> >> self.error_message = T('enter an integer')
> >> else:
> >> self.maximum = int(maximum)
> >> if error_message is None:
> >> error_message = T('enter an integer less than or equal
> >> to %(max)g')
> >> self.error_message = error_message %
> >> dict(max=self.maximum-1)
> >> elif maximum is None:
> >> self.minimum = int(minimum)
> >> if error_message is None:
> >> error_message = T('enter an integer greater than or equal
> >> to %(min)g')
> >> self.error_message = error_message % dict(min=self.minimum)
> >> else:
> >> self.minimum = int(minimum)
> >> self.maximum = int(maximum)
> >> if error_message is None:
> >> error_message = T('enter an integer between %(min)g and
> >> %(max)g')
> >> self.error_message = error_message % dict(min=self.minimum,
> >> max=self.maximum-1)
>
> >> And I have used it:
>
> >> Field('number', type='integer',
> >> requires=IS_INT_IN_RANGE(2,5,T=T), label=T('number')),
> >> ....
>
> >> It's full compatible. Are there any disadvantages in my solution? Or is
> >> there any other solution?
>
> >> Of course the whole file validators.py has to be changed. Maybe the file
> >> tools.py too?
> >> Is it possible to use this proposal in the next release?
>
> > --
> > Mit freundlichen Grüßen / With kind regards
> > Martin Weissenböck
> > Gregor-Mendel-Str. 37, 1190 Wien
> > Austria / European Union
> > Tel +43 1 31400 00
> > Fax +43 1 31400 700
>
>