colander icon indicating copy to clipboard operation
colander copied to clipboard

Translation domains of messages passed to validators

Open rkintzi opened this issue 12 years ago • 6 comments

Take a look at __call__ methods of Range validator:

if self.min is not None:
    if value < self.min:
        min_err = _(self.min_err, mapping={'val':value, 'min':self.min})
        raise Invalid(node, min_err)

Shoudn't there be:

        min_err = translationstring.TranslationString(self.min_err, 
                domain=self.min_err.domain, 
                default=self.min_err.default,
                mapping={'val':value, 'min':self.min})

rkintzi avatar Feb 18 '13 17:02 rkintzi

So it appears that if you pass a TranslationString into a TranslationString, it resolves as you would think. The properties of the inner TranslationString override the outer. So check out this code:

import colander
import translationstring

_ = translationstring.TranslationStringFactory('test')

schema = colander.SchemaNode(colander.Mapping(),
                             colander.SchemaNode(colander.Int(),
                                                 name='number',
                                                 validator=colander.Range(min=0, max=5, max_err=_('blah'))))

if __name__ == '__main__':
    try:
        data = schema.deserialize({'number': 10})
    except Exception as e:
        assert isinstance(e, colander.Invalid)

    # more specifically
    validator = colander.Range(min=0, max=5, max_err=_('blah'))
    assert validator.max_err.domain == 'test'

P.S. sorry for getting around to this so late.

jayd3e avatar Mar 18 '13 07:03 jayd3e

I have a validator:

def age_validator(node, value):
    if value <= 0:
        raise colander.Invalid(node, _(u"Age must be greater then 0"))

Error message is translated to polish, and in a browser I can see translation.

I have just checked that following version of valiadtor does not work (in the browser I see message stored in code).

def age_validator(node, value):
    validator = colander.Range(min=1, min_err=_(u"Age must be greater then 0"))
    assert (validator.min_err.domain == 'KonsultacjeNPR')
    validator(node,value)

For some reason it does not matter that translation domain is correct. The problem does not occur when you use the modified version of the range validator (as I described in first comment).

I am using translationstring-1.1 and colander-0.9.9 (with colander-1.0a2 I have the same problem).

rkintzi avatar Mar 18 '13 08:03 rkintzi

Ok I have your error reproduced. Going to attempt to find a solution for it.

jayd3e avatar Mar 18 '13 21:03 jayd3e

This issue resulted in a pull request to translationstring, changing the way TranslationStringFactory works. It can be found here: https://github.com/Pylons/translationstring/pull/12.

jayd3e avatar Mar 19 '13 18:03 jayd3e

Marking as "sprintable" to see if someone can determine if this is actually fixed.

mcdonc avatar Aug 10 '13 08:08 mcdonc

Seems to be fixed to me.

latteier avatar Apr 15 '14 18:04 latteier