On Wed, 2007-05-09 at 11:17 -0700, sandro dentella wrote:
> 
> Hi,
> 
>  the following code raises error as per subject:
> 
>    import re
>    from django.db import models
>    from django.newforms.models import form_for_model
>    from django.utils.translation import gettext_lazy as _
> 
> 
>    class Abc(models.Model):
>        choice = models.CharField(maxlength=10, help_text='xyz')
>        class Meta: app_label = 'xxx'
> 
>    form = form_for_model(Abc)()
>    field = form.fields['choice']
>    html = u'title="%s"' % re.sub('"', r'\"', _(field.help_text)  )
> 
> Traceback (most recent call last):
>   File "<stdin>", line 27, in ?
>   File "/usr/lib/python2.4/sre.py", line 142, in sub
>     return _compile(pattern, 0).sub(repl, string, count)
> TypeError: expected string or buffer
> 
> The problem is that _(field.help_text) is an instance of
> django.utils.functional.__proxy__ and that is not accepted by
> re.sub.
> 
> I'm also pretty sure that this same peace of code was working up to
> some
> days ago when I updated  to svn rel 5134.

This behaviour won't have changed any time recently.
> 
> Any hint on how to solve this probelm?

I think you may be a little stuck here. When you think through what is
meant to happen, the problem is quite hard to solve in the general case:
The gettext_lazy() function is only meant to perform its action
(replacing the string it represents with its translated counterpart)
when it is converted to a string, which is usually when it is rendered
to the template. This is so that it knows which locale is in effect at
the time.

Something like the re.sub(...) command you have given therefore would
need to become some kind of delayed evaluation function that meant
"convert the original string to its translated form and then apply
re.sub(...) to the result.

The one case where you can make this work is if the code you show above
is in the view function that is creating your template. At that point,
you know what the correct locale is, so you can explicitly convert the
gettext_lazy() result to a string (i.e. translate it) and then apply
re.sub() to it. However, if that was the case, you wouldn't be using
gettext_lazy in the first place in that line of code, so I suspect
you're out of luck there.

An alternative is that you could use the lazy() decorator to create a
second proxy that applied the function

        re.sub('"', r'\"', str(...))

to the initial __proxy__ object when it is time to convert it to a
string. I can't get this to work immediately. I would have thought
something like

        lazy(lambda s: re.sub('"', r'\"', str(s)))
        
would have worked, but it doesn't. I'll poke around a bit more when I
get some time to see if I can work something out, but it may not be
possible. The lazy() function is a bit awkward at times and may be
overkill for the purposes we need it for (it's causing no end of trouble
in the Unicode branch for similar reasons). You should experiment a bit,
too, since I'm pretty busy at the moment, so may not have time to get to
this for a little while.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to