Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1461 (closed enhancement: fixed)

Opened 12 years ago

Last modified 10 years ago

[PATCH] Genshi and i18n

Reported by: dbrattli Owned by: faide
Priority: normal Milestone: 1.1
Component: TurboGears Version: 1.0.3
Severity: normal Keywords: needs documentation
Cc: ricky


In order to get rid of explicit gettext _() calls in Genshi templates, one must attach a Translator filter to Genshi ( http://genshi.edgewall.org/wiki/Documentation/i18n.html). Attaching the translator (and other) filters is currently not possible in TurboGears. A normal way to add genshi filters is to specify a callback to the TemplateLoader?. The attached patch makes it possible to add a callback in order to add Genshi filters.

Here is example code from controllers.py that shows how to add a gettext translator filter to Genshi once the patch has been applied:

from turbogears.i18n import gettext
from genshi.filters import Translator

def genshi_loader_callback(template):
	template.filters.insert(0, Translator(gettext))

config.update({'genshi.loader_callback' : genshi_loader_callback})


base.patch Download (669 bytes) - added by dbrattli 12 years ago.
Patch for turbogears/view/base.py
base2.patch Download (1.6 KB) - added by dbrattli 11 years ago.
New and revised patch
base-genshi-i18n-tg11.diff Download (1.4 KB) - added by dbrattli 11 years ago.
New version, trying to comply with guidelines. Patch against turbogears-1.1.

Change History

Changed 12 years ago by dbrattli

Patch for turbogears/view/base.py

comment:1 Changed 12 years ago by dbrattli

The 'loader_callback' option is available with Genshi-0.4.3.

comment:2 Changed 12 years ago by dbrattli

  • Summary changed from Genshi and i18n to [PATCH] Genshi and i18n

comment:3 Changed 12 years ago by ricky

  • Cc ricky added

comment:4 Changed 12 years ago by faide

  • Milestone changed from 1.0.4 to 1.1

This would need some documentation in the wiki, but the patch is definitely interesting.

As it is an enhancement and there is another solution right now I move this to 1.1 target.

comment:5 Changed 12 years ago by faide

Applied in r3760. Could you please add some documentation on the useage of this option in our wiki ? I'll leave the ticket open so we add this documentation and don't forget about this.

comment:6 Changed 11 years ago by faide

  • Milestone changed from 1.5 to 1.1

comment:7 Changed 11 years ago by Chris Arndt

  • Milestone changed from 1.1 to 1.1 beta 2

comment:8 Changed 11 years ago by Chris Arndt

  • Keywords needs documentation added

comment:9 Changed 11 years ago by kvdb

I'm trying to figure out how this is related to tg-admin i18n collect not collecting genshi template strings. Looks like that's still a few steps ahead. Is it planned for the 1.1 branch eventually?

comment:10 Changed 11 years ago by Chris Arndt

Yes, we just haven't got around to doing it yet. AFAICS, there are two things which need to be done.

  1. Actually look for Genshi files (*.html) when collecting.
  2. Implement an equivalent of the command.i18n.InternationalizationTool.scan_kid_files method.

comment:11 Changed 11 years ago by faide

the i18n collection task is reported in #1733

comment:12 Changed 11 years ago by faide

we need to integrate the genshi i18n filter in the same way we add the kid filter in case the user activates i18n in the configuration.

comment:13 Changed 11 years ago by faide

we should amend the load_engines function in the turbogears/view/base.py file to load the genshi filter.

We can not disable either because people can do @expose(template="kid:xxx") and we need i18n to work also for the those cases...

comment:14 Changed 11 years ago by dbrattli

Sounds good. I would suggest that we look at the i18n.run_template_filter config setting and only insert the genshi filter if it's set. That way we do not need to write up any more documentation than there already is, since things should work the same way as for Kid. Developers then don't have to set up the filter themselves. Something like:

    def genshi_loader_callback(template):
	template.filters.insert(0, Translator(gettext))

    get = config.get
    engine_options = {
    if get("i18n.run_template_filter", False):
        engine_options["genshi.loader_callback"] = genshi_loader_callback

To early in the morning for me to find a way to write that "if" inside the engine_options dict ;-)

comment:15 Changed 11 years ago by faide

  • Owner changed from anonymous to faide
  • Status changed from new to assigned

comment:16 Changed 11 years ago by faide

Added support in the web interface (and i18n command line at the same time) to extract text from the genshi templates.

We now just need the filter activated as mentioned above... I'll see to that tomorrow

Changed 11 years ago by dbrattli

New and revised patch

comment:17 Changed 11 years ago by dbrattli

Added a new and revised patch (base2.patch). The goals are:

  1. Only insert Genshi Translator filter if i18n.run_template filter is set
  1. Only override gensh.loader_callback if not already set. This makes it possible for the developer to get different behaviour, use a different gettext etc.

If it looks good I can apply it.

comment:18 Changed 11 years ago by Chris Arndt

Please see  http://docs.turbogears.org/patching_guidelines, esp. the points about PEP8 compliance and proper location for running "svn diff". Thanks.

Changed 11 years ago by dbrattli

New version, trying to comply with guidelines. Patch against turbogears-1.1.

comment:19 Changed 11 years ago by faide

@dbrattli: I applied and tested your patch because I needed it to continue my work on the i18n refactoring. What would be über nice though would be some docs addition to explain the genshi.loader_callback usage (ie: a use case were we would need it)

Thanks for the Translator work Dag!!

comment:20 Changed 11 years ago by dbrattli

I'm a bit unsure about what to do next. There's already a document that describes the genshi.loader_callback at  http://docs.turbogears.org/1.0/GenshiInternationalizationWithBabel, but I don't think it's necessary since the latest patch (base-genshi-i18n-tg11.diff) hides the details of the genshi.loader_callback from the user, and your other string extraction stuff takes care of the rest. So it's only necessary to configure i18n.run_template_filter as decribed in  http://docs.turbogears.org/1.0/Internationalization. Thus I think we only need to make a 1.1 (genshi instead of kid) version of the Internationalization page. Just a few changes needed, "xml:lang" instead of "lang" etc, or did you have something else in mind?

comment:21 Changed 11 years ago by faide

Well, in fact along the road of i18n refactoring I stumbled upon the sogettext system and tried to unravel its mysteries... I decided the create an sagettext to be the pendant of the so one. But I'm still a bit puzzled on the way to actually use the thingy... At the moment if you try to activate sogettext or sagettext you won't be able to enter the toolbox because it will yell at you telling that the tables have not been created.

I am trying to figure out a way to easily create the tables if you activate the option in the config file... and then a way to populate the sql tables with actual gettext data.

The second important point is that I want to review how the compiled gettext resources are packages and see how this should be documented for a deployment behind mod_wsgi.

Obviously if we bundle the gettext resources inside the egg we'll be mod_wsgi ready and "finished" applications will "just work". BUT we have users out there that deploy TG behind mod_wsgi and then mount the toolbox from their controllers and use the provided tools.

In this use case we have an issue because we need to make sure Apache has read/write access to the directory where we want to manipulate our mo/po files. This goes against the idea of bundling a finished application as a standalone egg and deploying that to production.

comment:22 Changed 10 years ago by Chris Arndt

  • Milestone changed from 1.1b4 to 1.1

comment:23 Changed 10 years ago by Chris Arndt

  • Status changed from assigned to closed
  • Resolution set to fixed

The original issue is solved (Genshi string collection see r5799, Genshi translation filter see r5714). No documentation necessary, since the interface is the same as for Kid.

Florents last comment (if I understand correctly) concerns only the toolbox and should be handled in a separate ticket.

I'm closing this ticket as fixed and opened a collection ticket for remaining Genshi i18n issues (#2369).

Note: See TracTickets for help on using tickets.