i18n, FormEncode, Genshi

14 views
Skip to first unread message

Daniel Haus

unread,
Aug 27, 2007, 9:14:08 AM8/27/07
to pylons-discuss
Hi!

Being fairly new to Pylons, I'm working on an Pylons-based (0.9.6rc3)
website that utilizes Genshi, Babel, ToscaWidgets and FormEncode. So
far everything is working fine except for the translations. I was able
to extract and compile language catalogs as described in
http://wiki.pylonshq.com/display/pylonsdocs/Internationalization+and+Localization
.

Problem #1:
FormEncode's error messages are not translated at all. What are the
required steps?

Problem #2:
Although all strings are correctly extracted from my Genshi templates,
the strings aren't translated in the rendered output. Do I somehow
have to apply the genshi.filters.i18n.Translator filter manually (if
so, what would be the easiest way?) or am I missing something else?

I've been searching all related documentation and lists/groups for
hours without success. Maybe anyone can give some hints?

Apart from these issues I'm really impressed how Pylons encourages
writing clean and readable code without sacrificing flexibility at
all.

Thank you,
Daniel Haus

Alberto Valverde

unread,
Aug 27, 2007, 11:36:27 AM8/27/07
to pylons-...@googlegroups.com

On Aug 27, 2007, at 3:14 PM, Daniel Haus wrote:

>
> Hi!
>
> Being fairly new to Pylons, I'm working on an Pylons-based (0.9.6rc3)
> website that utilizes Genshi, Babel, ToscaWidgets and FormEncode. So
> far everything is working fine except for the translations. I was able
> to extract and compile language catalogs as described in
> http://wiki.pylonshq.com/display/pylonsdocs/Internationalization+and
> +Localization
> .
>
> Problem #1:
> FormEncode's error messages are not translated at all. What are the
> required steps?

You can use the new "state" argument to pylons' "validate" decorator
(in trunk only since r2347) and set its "_" attribute:

from pylons.i18n import _
from pylons.decorators import validate

class State:
_ = _

class AController(BaseController):
@validate(state=State(), ...)
def my_meth(self):
...

If you're using ToscaWidgets, its "validate" decorator (in
toscawidgets.mods.pylonshf) accepts a "state_factory" argument with
similar semantics to Pylons' "state" but accepts a callable instead
that returns the state:

from toscawidgets.mods.pylonshf import validate

class AController(BaseController):
@validate(state_factory= lambda: State(), ...)
def my_meth(self)
...

You'll then have to add FormEncode's messages to your app's message
catalog, compile it, etc... and that should do it.

BTW, I'd love to hear of a way to configure Pylons's "_" function to
use the message catalog inside FormEncode's egg instead of copying it
inside my app's catalog, is there any?

HTH,
Alberto

Ben Bangert

unread,
Aug 27, 2007, 11:58:15 AM8/27/07
to pylons-...@googlegroups.com
On Aug 27, 2007, at 8:36 AM, Alberto Valverde wrote:

> You'll then have to add FormEncode's messages to your app's message
> catalog, compile it, etc... and that should do it.
>
> BTW, I'd love to hear of a way to configure Pylons's "_" function to
> use the message catalog inside FormEncode's egg instead of copying it
> inside my app's catalog, is there any?

I'm thinking there should be an option, that when 'on', will pass the
State class into @validate for you. I'm also curious about whether
there's some way to collect the message catalogs used in packages by
your app. Maybe that's a Babel thing, that it could look at your
dependency tree and find message catalogs to consolidate for you?

Cheers,
Ben

Daniel Haus

unread,
Aug 27, 2007, 4:44:48 PM8/27/07
to pylons-discuss
Thank you for your help Alberto, once again. I think Ben is right,
there should be a somewhat more elegant solution to this. It would be
great to have it all done automatically and out of my controller code,
like it works in TG. I will see if I can find a way.

Now there's still this Genshi translation-filter issue. The Pylons
i18n tutorial does cover message extraction from Genshi, but there are
no hints on configuration of that filter, which i think would be
necessary. Is this a common problem, or am I missing something
obvious?

Greetings,
Daniel

> smime.p7s
> 3KHerunterladen

Max Ischenko

unread,
Aug 27, 2007, 11:32:59 PM8/27/07
to pylons-...@googlegroups.com
Hi Alberto,

On 8/27/07, Alberto Valverde <alb...@toscat.net> wrote:

You'll then have to add FormEncode's messages to your app's message
catalog, compile it, etc... and that should do it.

BTW, I'd love to hear of a way to configure Pylons's "_" function to
use the message catalog inside FormEncode's egg instead of copying it
inside my app's catalog, is there any?

I submitted a patch that does roughly this, see
http://pylonshq.com/project/pylonshq/ticket/296

Max.


Alberto Valverde

unread,
Aug 28, 2007, 10:06:50 AM8/28/07
to pylons-...@googlegroups.com
Yeah, this is more or less what I was talking about. However, my thoughts were more on the direction of the "domain" kw. that TG's gettext had (and that formencode is built around) which lets you choose which egg's catalog to use for translations.

OTOH, Ben's suggestion of an utility that consolidated all the messge catalogs used by the application into one sounds like the best idea to me since it gives you full control of the final catalog (eg: to override ceratin strings) and would scale better when using various eggs each with their own catalog (although none implemented yet, widget packages come to mind...)

I haven't used Babel yet so I don't feel I'm in the best position to open a feature request for this at their tracker just now... perhaps someone that also thinks it's a good idea can do it? :)

Albert

Alberto Valverde

unread,
Aug 28, 2007, 10:18:34 AM8/28/07
to pylons-...@googlegroups.com

On Aug 27, 2007, at 10:44 PM, Daniel Haus wrote:
>
> Now there's still this Genshi translation-filter issue. The Pylons
> i18n tutorial does cover message extraction from Genshi, but there are
> no hints on configuration of that filter, which i think would be
> necessary. Is this a common problem, or am I missing something
> obvious?

Take a look at http://genshi.edgewall.org/wiki/Documentation/
i18n.html#translation and the source of genshi's Buffet plugin.
Apparently (haven't tried, so tell us how it goes...:) you should be
able to do something like:

from genshi.filters import Translator
from pylons.i18n import _

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

Then you would need to pass this callback when initializing genshi's
buffet plugin:

inside middleware.py (or load_environment):

options = {"genshi.loader_callback": template_loaded}
# options.update(options_from_config_file)
config.add_template_engine("genshi", "yourapp.templates", options)

HTH,
Alberto

Daniel Haus

unread,
Aug 29, 2007, 5:53:36 AM8/29/07
to pylons-discuss
Great! I'll have a try!

Daniel

On 28 Aug., 16:18, Alberto Valverde <albe...@toscat.net> wrote:
> On Aug 27, 2007, at 10:44 PM, Daniel Haus wrote:
>
>
>
> > Now there's still this Genshi translation-filter issue. The Pylons
> > i18n tutorial does cover message extraction from Genshi, but there are
> > no hints on configuration of that filter, which i think would be
> > necessary. Is this a common problem, or am I missing something
> > obvious?
>

> Take a look athttp://genshi.edgewall.org/wiki/Documentation/

Daniel Haus

unread,
Aug 29, 2007, 9:36:09 AM8/29/07
to pylons-discuss
Works perfectly, thank you very much, Alberto!
Reply all
Reply to author
Forward
0 new messages