Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Pyramid status update...
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeff Madynski  
View profile  
 More options Jan 12 2011, 2:16 pm
From: Jeff Madynski <jeff...@gmail.com>
Date: Wed, 12 Jan 2011 11:16:06 -0800
Local: Wed, Jan 12 2011 2:16 pm
Subject: Re: Pyramid status update...
Chris,
I can possibly provide pyramid-jinja2 i18n docs, but I am not sure if
I've taken a decent approach.

I started following the project a few weeks ago. I like it.  While
reading through the docs, I was playing along by modifying shootout
(https://github.com/jeffmad/shootout).  I changed it to use jinja2,
repoze.who 2.0, and beaker, also changed some things to look like
1.0a9. It was fun to learn things by changing them.

Then I thought it would be interesting to try to make a spanish version.
I followed along on
http://docs.pylonshq.com/pyramid/dev/narr/i18n.html and
http://jinja.pocoo.org/extensions/

For jinja2, in addition to pyramid instructions for i18n, I added
1) a message extractor in setup.py  ('**.jinja2',
'jinja2.ext:babel_extract', None)
2) enable jinja2 i18extension in development.ini (below)
3) put {%trans %} tags in the templates.

Then after i had everything in place and went to test, I got stuck.
It did not work.  Appending _LOCALE_=es was not doing anything. After
poking around quite a bit adding logging in pyramid/i18n.py and
pyramid_jinja/__init__.py,  I figured out that the Translations object
I was passing to jinja2 had  domain=messages, so nothing got
translated in jinja2.  I am still not sure why fallback didn't work.

In any case, I ended up getting it working by
env.install_gettext_callables() in Jinja2TemplateRenderer __call__
(below).  Since jinja2 doesn't pass the domain in, this patch sets it
to "shootout"

I am not really comfortable with this since changing the environment
on every request seems wrong and I don't understand what concurrency
problems may be lurking.

Another approach might be to force the domain to be listed in each
tag, but that seems ugly.  On my next app, I'll be a lot more open to
chameleon since you do not have to translate strings in the py code
like you do in jinja2.  Most of those translations should be moved to
the template, but I am warming up to chameleon.

I can try to condense my experience in an rst and point you to it if
you think I am on the right track. Any suggestions appreciated.

--jeff

development.ini

[app:shootout]
use = egg:shootout
jinja2.extensions = jinja2.ext.i18n

#new imports
from pyramid.i18n import get_localizer
from pyramid.i18n import get_locale_name
from pyramid.i18n import Translations
import logging

inside Jinja2TemplateRenderer

    def __call__(self, value, system):
        try:            system.update(value)
        except (TypeError, ValueError):
            raise ValueError('renderer was passed non-dictionary as value')

#start i18 patch here
        # TODO do this stuff only when jinja2.i18n.ext is loaded
        localizer = get_localizer(system['request'])
        localeName = get_locale_name(system['request'])
        settings = system['request'].registry.settings
        default_locale_name = settings.get('default_locale_name', 'en')
        if localeName == default_locale_name:
            self.environment.install_gettext_callables(localizer.translations.ugettext,
localizer.translations.ungettext, newstyle=True)
        else:
            # TODO cannot hardcode domain here, must get it by lookup
config or lookup in translations
            def mydgettext(message):
                return localizer.translations.dugettext(u'shootout', message)
            def mydngettext(singular, plural, num):
                return
localizer.translations.dungettext(u'shootout',singular, plural, num )
            self.environment.install_gettext_callables(mydgettext,
mydngettext, newstyle=True)
# finish i18n patch here
        result = self.template.render(system)
        return result

Lastly, must make sure {{ gettext(msg, domain) }} works in jinja2

> - Explain how to use i18n localization in Mako and Jinja2.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.