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.

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.

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.

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

Reply via email to