I think I'm using the correct pyramid APIs to get the translator.
Here's how I do it:

def get_deform_translator(request):
    tsf_deform = translationstring.TranslationStringFactory('deform')
    return lambda s, m=None:
pyramid.i18n.get_localizer(request).translate(tsf_deform(s, m))

And I follow recipe
http://docs.pylonsproject.org/projects/pyramid_cookbook/dev/i18n.html
to attach a localizer to a request (which is retrieved in the above
function).

The problem still stands: One chameleon TemplateLoader instance (e.g.
the one in deform.template.default_renderer) doesn't work, because it
caches PageTemplateFile instances per filename and disregards
potentially different translators - i.e. it will just keep the
PageTemplateFile with the translator passed at the time it was put in
the loader's registry. So when a form with invalid data is submitted
using a different locale, the error messages will be in the wrong
language.
What I do now looks like this (in the main function of my pyramid app):

    settings['deform_renderers'] = {}
    for locale in settings['msp.main.langs.list']:
        settings['deform_renderers'][locale] =
deform.template.ZPTRendererFactory(
                (pkg_resources.resource_filename('gulpdeform',
'templates'), deform.template.default_dir),
                auto_reload=False,
                debug=False,
        )

    config = Configurator(
        settings=settings
    )

Ideally I would want to pass a translator for each locale right away,
but I wanted to use pyramid's make_localizer function, which isn't
useful until later in main, when I'm done registering translation
directories. So instead I assign the translator the first time I'm
using the renderer, before instantiating a deform.Form:

            if not renderer.translate:
                t = get_deform_translator(request)
                renderer.translate = t
                renderer.loader.kwargs['translate'] = ChameleonTranslate(t)

This works well enough to not feel like a problem anymore; it just
looks hacky. So for my use case, a TemplateLoader which accepts a
translator as parameter to the load method would be the better API -
but may be impossible, because a translator cannot be used as
dictionary key ... Having to pass the translator as constructor
argument to TemplateLoader means I must have one TemplateLoader
instance per translator.

best,
robert

On Fri, Aug 12, 2011 at 10:54 AM, Malthe Borch <[email protected]> wrote:
> On 12 August 2011 10:05, Robert Forkel <[email protected]> wrote:
>> hm. I do not really follow. I'll try to explain my problem in more
>> detail. First, I only use Chameleon from within deform, the rest of my
>> app uses mako. Now I noticed that form rendering was slow and that was
>> because of the template loader parsing the templates all the time.
>> As far as I can see in chameleon.loader, the caching is done per
>> instance of TemplateLoader. Now deform instantiates a TemplateLoader
>> when a deform.template.ZPTRendererFactory is instantiated. When using
>> the default_renderer this is only done once on import of deform and
>> caching will work - i guess. But to be able to pass a translator per
>> request, I instantiate a new ZPTRendererFactory per request as well -
>> and no caching is possible.
>
> I see.
>
> What I can tell you is that Pyramid wants to do this for you, i.e. it
> automatically sets a translator for you. Instead of trying to do this
> yourself, it would be better to use the built-in language negotiation.
>
> If you're still unsure, maybe let's try and see your code to see if
> something's funky in your setup.
>
> \malthe
>
> --
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" 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/pylons-discuss?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en.

Reply via email to