i18n questions

6 views
Skip to first unread message

cd34

unread,
Aug 24, 2008, 11:32:19 PM8/24/08
to TurboGears
Running into an issue with i18n that I'm reasonably sure is something
simple I'm missing. extract_messages finds the phrases from the html,
update_catalog, modify the .po file, compile_catalog, hit a few test
URLs and the .mo file is used only for translations in the
controllers.

Relevant details:

Turbogears 1.9.7a3
Babel-0.9.3-py2.5.egg
Genshi-0.5.1-py2.5-linux-i686.egg
Mako-0.2.2-py2.5.egg
Pylons-0.9.7rc1-py2.5.egg

Followed the instructions on http://www.turbogears.org/2.0/docs/main/Internationalization.html

Made one minor change to setup.py uncommenting:

message_extractors = {'cptest': [
('**.py', 'python', None),
('templates/**.mak', 'mako', None),
('templates/**.html', 'genshi', None),
('public/**', 'ignore', None)]},

(also fixed the extension for mako which defaults to .mako, while TG2
looks for .mak)

$ python setup.py extract_messages

...
extracting messages from cptest/templates/__init__.py
extracting messages from cptest/templates/about.html
extracting messages from cptest/templates/login.html
...

It does pull the messages from about.html

$ python setup.py update_catalog -l es
running update_catalog
updating catalog 'cptest/i18n/es/LC_MESSAGES/cptest.po' based on
'cptest/i18n/cptest.pot'

does properly list the messages

#: cptest/controllers/root.py:18
msgid "Homepage"
msgstr "página principal"

#: cptest/templates/about.html:14
msgid "Learning TurboGears 2.0: Quick guide to the Quickstart pages."
msgstr "spanish learning turbogears 2.0 quick guide"

$ python setup.py compile_catalog
running compile_catalog
2 of 95 messages (2%) translated in 'cptest/i18n/es/LC_MESSAGES/
cptest.po'
compiling catalog 'cptest/i18n/es/LC_MESSAGES/cptest.po' to 'cptest/
i18n/es/LC_MESSAGES/cptest.mo'

compile_catalog does recognize the messages

from development.ini:

[app:main]
lang = es

I've also set my browser to prefer Spanish (es)

from root.py

@expose('cptest.templates.index')
def index(self):
return dict(page=_('Homepage'))

This does work and the page properly displays:

Now Viewing: página principal

So, i18n works to this point. Phrases translated within controllers
using _() notation translate properly. Even removing lang=es from
development.ini works properly with my browser set to prefer Spanish
(es). However, translations within the page do not work.

Even a simple change to the login.html is not reflected in the
rendered version, but, it does appear to be properly inserted in
the .mo file. pybabel does appear to have a few problems parsing the
genshi templates due to some of the nesting and xhtml markup, but, it
appears to have not had any problems with the login.html template
which is why I tried it first. While extract_messages appears to scan
the .mak template file, I was unable to get pybabel to catalog the
text portions from it.

As far as I can tell, TG2 does appear to pass everything through
pylons which passes it through the i18n translator. The filter does
appear to be installed in the pylons filter path. Have I missed
something, or is i18n broken?

Timur Izhbulatov

unread,
Sep 23, 2008, 6:49:10 AM9/23/08
to TurboGears
Unfortunately, the root cause of this issue is rather difficult to
identify.

In short, the simplest solution is overriding the
AppConfig.setup_default_renderer() method in yourproject/config/
app_cfg.py:

# ...

class MyAppConfig(AppConfig):
def setup_default_renderer(self):
from pylons.i18n import ugettext
from genshi.filters import Translator

def template_loaded(template):
template.filters.insert(0, Translator(ugettext))

options = {"genshi.loader_callback": template_loaded}
config['buffet.template_options'].update(options)
super(MyAppConfig, self).setup_default_renderer()

base_config = MyAppConfig()
base_config.renderers = []
base_config.package = yourproject

#Set the default renderer
base_config.default_renderer = 'genshi'
# ...

The long answer.

If you look at the source code of tg.configuration, you will notice
that it configures several template engines including genshi and even
add the translator filter to its loader. But by default TG 2 uses
legacy renderer (Buffet), and the setup_default_renderer() method
which is called after all engines are configured seems to override
what has been set up earlier.

Simply disabling legacy renderer in app_cfg won't work since the
setup_genshi_renderer() method is broken. It defines a loader callback
but doesn't actually pass it to the loader, and the callback itself is
also broken since it works with the wrong object (genshi.template
instead of template). And even if you fix these bugs, template loading
still won't work. I gave up fixing things beyond this point and
returned to the above solution.

Good luck!

On Aug 25, 7:32 am, cd34 <mcd...@gmail.com> wrote:
> Running into an issue with i18n that I'm reasonably sure is something
> simple I'm missing.  extract_messages finds the phrases from the html,
> update_catalog, modify the .po file, compile_catalog, hit a few test
> URLs and the .mo file is used only for translations in the
> controllers.
>
> Relevant details:
>
> Turbogears 1.9.7a3
> Babel-0.9.3-py2.5.eggGenshi-0.5.1-py2.5-linux-i686.egg
> Mako-0.2.2-py2.5.egg
> Pylons-0.9.7rc1-py2.5.egg
>
> Followed the instructions onhttp://www.turbogears.org/2.0/docs/main/Internationalization.html
> So,i18nworks to this point.  Phrases translated within controllers
> using _() notation translate properly.  Even removing lang=es from
> development.ini works properly with my browser set to prefer Spanish
> (es).  However, translations within the page do not work.
>
> Even a simple change to the login.html is not reflected in the
> rendered version, but, it does appear to be properly inserted in
> the .mo file.  pybabel does appear to have a few problems parsing thegenshitemplates due to some of the nesting and xhtml markup, but, it
> appears to have not had any problems with the login.html template
> which is why I tried it first.  While extract_messages appears to scan
> the .mak template file, I was unable to get pybabel to catalog the
> text portions from it.
>
> As far as I can tell, TG2 does appear to pass everything through
> pylons which passes it through thei18ntranslator.  The filter does
Reply all
Reply to author
Forward
0 new messages