Localized URL schema using chameleon

21 views
Skip to first unread message

Oscar Curero

unread,
Aug 20, 2014, 1:21:47 PM8/20/14
to pylons-...@googlegroups.com
Hi,

I'm building my first application using pyramid and chameleon and I'm having problems with the localized URL schema. It's something like this:

/en/products
/es/productos
/ca/productes

The problem starts when I need to make the HTML templates. For example, I want to make the following template:

<p><a href="/${request.locale_name}/${products}"><img src="products.jpg" alt="" /></a></p>

That template should render the link depending on the user locale setting ("request.locale_name" is a variable where I save the locale name and "products" is a translation string), ie:

For "en" locale --> <p><a href="/en/products"><img src="products.jpg" alt="" /></a></p>
For "es" locale --> <p><a href="/es/productos"><img src="products.jpg" alt="" /></a></p>
For "ca" locale --> <p><a href="/ca/productes"><img src="products.jpg" alt="" /></a></p>

However, what I get is:

NameError: products

 - Expression: "/${request.locale_name}/${products}"
 - Filename:   ... ate/templates/products.pt
 - Location:   (line 10: col 30)
 - Source:     ... <p><a href="/${request.locale_name}/${products}"><img ...

Reading the page "http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/narr/i18n.html#chameleon-template-support-for-translation-strings" I get he feeling that what I'm trying to achieve is possible. What do I'm doing wrong?

Thanks in advance,

Wichert Akkerman

unread,
Aug 20, 2014, 1:28:28 PM8/20/14
to pylons-...@googlegroups.com

> On 20 Aug 2014, at 19:21, Oscar Curero <flex...@gmail.com> wrote:
>
> Hi,
>
> I'm building my first application using pyramid and chameleon and I'm having problems with the localized URL schema. It's something like this:
>
> /en/products
> /es/productos
> /ca/productes
>
> The problem starts when I need to make the HTML templates. For example, I want to make the following template:
>
> <p><a href="/${request.locale_name}/${products}"><img src="products.jpg" alt="" /></a></p>

The problem here is that ${products} assumes that you have a python variable named “products” at hand and want to insert its value in the template. But since you have no such variable you get a NameError exception. If you goal is to make the word “products” translatable you can do this: ${_(“products”)} .

Wichert.

Oscar Curero

unread,
Aug 20, 2014, 7:13:20 PM8/20/14
to pylons-...@googlegroups.com

Thanks Wichert. If I try what you say with nothing else, it doesn't work yet:

NameError: _

 - Expression: "${request.locale_name}/${_('products')}"
- Filename: ... ate/templates/products.pt - Location: (line 10: col 30) - Source: ... <p><a href="/${request.locale_name}/${_('products')}"><img ...

However, if I add at the top of the template the following statement, it works as expected:

<?python from pyramid.i18n import TranslationString as _ ?>

Is this the way it should work?

Thanks!

Kamal Gill

unread,
Aug 20, 2014, 7:29:29 PM8/20/14
to pylons-...@googlegroups.com
Oscar,

It's probably safer to pass the translated string as a template variable from the view callable, since Pyramid's i18n machinery only picks up translation strings marked via i18n:translate and i18n:attributes in .pt files IIRC.

Basically, you would pass "products" as a string marked for translation to your template from the view.

The template would be the following (using your original code)...

<p><a href="/${request.locale_name}/${products}"><img src="products.jpg" alt="" /></a></p> 

While the view would mark the "products" string for i18n as follows…

from pyramid.i18n import TranslationString as _
@view_config(route_name='products', renderer='templates/products.pt')
def products_view():
    ...
    return dict(products=_(u'products'))

HTH,
Kamal


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Wichert Akkerman

unread,
Aug 21, 2014, 2:32:39 AM8/21/14
to pylons-...@googlegroups.com
It is! You can also use a BeforeRender event to expose _ to all templates. See http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/api/events.html#pyramid.events.BeforeRender for an example of doing that.

Wichert.

Reply all
Reply to author
Forward
0 new messages