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 received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-de...@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to