Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Translation domains of messages passed to validators #83

Open
rkintzi opened this Issue · 6 comments

4 participants

@rkintzi

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})
@jayd3e
Collaborator

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.

@rkintzi

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 rkintzi closed this
@rkintzi rkintzi reopened this
@jayd3e
Collaborator

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

@jayd3e
Collaborator

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

@mcdonc
Owner

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

@latteier

Seems to be fixed to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.