> On 21 Aug 2014, at 01:29, Kamal Gill <[email protected]> wrote: > > 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 > <http://products.pt/>') > def products_view(): > ... > return dict(products=_(u'products')) > > HTH, > Kamal > > > On Aug 20, 2014, at 4:13 PM, Oscar Curero <[email protected] > <mailto:[email protected]>> wrote: > >> >> >> On Wednesday, August 20, 2014 7:28:28 PM UTC+2, Wichert Akkerman wrote: >> >> > On 20 Aug 2014, at 19:21, Oscar Curero <[email protected] <javascript:>> >> > 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. >> >> 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 <http://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?
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 <http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/api/events.html#pyramid.events.BeforeRender> for an example of doing that. Wichert. -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pylons-discuss. For more options, visit https://groups.google.com/d/optout.
