problem with i18n in chameleon zpt

99 views
Skip to first unread message

Geo

unread,
May 30, 2011, 2:45:26 PM5/30/11
to pylons-discuss
I'm having issue with activating the translation. I created a sample
project with paster create -t pyramid_routesalchemy, and I gone
through the following steps to setup the i18n:

1. changed the development.ini:
default_locale_name = zh

2. changed in the the mytemplate.pt:
<h2 i18n:translate="search-document">Search documentation</h2>
...
<p class="app-welcome" i18n:translate="">
Welcome to <span class="app-name" i18n:name='app-name'>$
{project}</span>, an application generated by<br/>
the Pyramid web application development framework.
</p>

3. easy_install "chameleon<1.999"
4. changed in the setup.py to make babel to consider **.pt files when
doing message id extraction.
5. python setup.py extract_messages
6. python setup.py init_catalog -l zh
7. make the translation in chinese in the po file
8. compile_catalog
9. changed in the __init__.py:
config.add_translation_dirs('myproject:locale/')

after all the above steps, I ran the server and open the page, but I
couldn't see my translation, even with http://localhost:6543/?_LOCALE_=zh

Can anybody in the group help me with this problem?


Douglas Cerna

unread,
May 30, 2011, 11:06:44 PM5/30/11
to pylons-...@googlegroups.com
On Mon, May 30, 2011 at 12:45 PM, Geo <geo...@gmail.com> wrote:
> 2. changed in the the mytemplate.pt:
> <h2 i18n:translate="search-document">Search documentation</h2>
> ...
>        <p class="app-welcome" i18n:translate="">
>          Welcome to <span class="app-name" i18n:name='app-name'>$
> {project}</span>, an application generated by<br/>
>          the Pyramid web application development framework.
>        </p>

Make sure you specify the i18n namespace and your application domain
in the template. Something like:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="mypyramidapp">

Hope that helps you.

Douglas

Geo

unread,
May 31, 2011, 9:36:16 PM5/31/11
to pylons-discuss
That partially solved the problem. For the following html code:

<h2 i18n:translate="search-document">Search documentation</h2>

the corresponding text in po is:
msgid "search-document"
msgstr "Search it!"

and that works fine,

but the other part, which is:
<p class="app-welcome" i18n:translate="">
Welcome to <span class="app-name" i18n:name="app">${project}</
span>, an application generated by<br/>
the Pyramid web application development framework.
</p>

and the corresponding po text:
msgid ""
"Welcome to ${appname} an application generated by the Pyramid web "
"application development framework. "
msgstr "Welcome to ${appname}!"

never showed up in the rendering result. Would you give me further
assistance to help me out?

Jan Dittberner

unread,
May 31, 2011, 5:02:42 PM5/31/11
to pylons-discuss
On 31 Mai, 03:06, Douglas Cerna <replaceaf...@gmail.com> wrote:
> Make sure you specify the i18n namespace and your application domain
> in the template. Something like:
>
> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
> xmlns:tal="http://xml.zope.org/namespaces/tal"
> xmlns:i18n="http://xml.zope.org/namespaces/i18n"
> i18n:domain="mypyramidapp">

I have a similar issue. My template looks like this:

-- 8< -------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
metal:use-macro="base"
i18n:domain="invoicer">
<tal:block metal:fill-slot="content">
<h3 i18n:translate="">Invoice details for ${invoice.invoicenr}</
h3>
<dl>
<dt>Customer:</dt>
<dd>${invoice.customer.name} (${invoice.customer.customertype})</
dd>
</dl>
</tal:block>
</html>
-- 8< -------------------------------------------

The request locale is initialized in a event subscriber:

-- 8< -------------------------------------------
from pyramid.events import NewRequest
from pyramid.events import subscriber


@subscriber(NewRequest)
def set_request_locale(event):
request = event.request
from pyramid.threadlocal import get_current_registry
settings = get_current_registry().settings
languages = settings['available_languages'].split()
for lang in request.accept_language.best_matches():
if lang in languages:
request._LOCALE_ = lang
break
else:
request._LOCALE_ = settings['default_locale_name']
-- 8< -------------------------------------------

The translation of strings that are defined in Python code and
translated using get_localizer(request).translate(_('Customers'))
works correctly.
_ is initialized as _ = TranslationStringFactory('invoicer').


Any help would be appreciated.

Regards
Jan Dittberner

Douglas Cerna

unread,
Jun 1, 2011, 12:48:24 AM6/1/11
to pylons-...@googlegroups.com
2011/5/31 Geo <geo...@gmail.com>:

> but the other part, which is:
> <p class="app-welcome" i18n:translate="">
>   Welcome to <span class="app-name" i18n:name="app">${project}</
> span>, an application generated by<br/>
>   the Pyramid web application development framework.
> </p>

Ah! I remember this one... Try:

----- %< -----


<p class="app-welcome" i18n:translate="">

Welcome to <span class="app-name" i18n:name="app">${project}</span>,
an application generated by<br i18n:name="break"/> the Pyramid web
application development framework.</p>
----- %< -----

Note the use of i18n:name for all the inline markup (including break
tags) and no newline at the end of the element with i18n:translate.

I still wonder what's the right solution for this...

Douglas

Malthe Borch

unread,
Jun 1, 2011, 2:05:49 AM6/1/11
to pylons-...@googlegroups.com
It might be helpful if there was a debug-version of the (default)
translation function which logged or recorded succesful and failed
translations.

\malthe

> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>
>

george hu

unread,
Jun 1, 2011, 2:23:44 AM6/1/11
to pylons-...@googlegroups.com
Nope, this will produce the following po text:

msgid ""
"Welcome to ${app}, an application generated by${break} the Pyramid web "
"application development framework. "
msgstr ""

and it doesn't work either.


Douglas

Douglas Cerna

unread,
Jun 1, 2011, 3:06:08 AM6/1/11
to pylons-...@googlegroups.com
On Wed, Jun 1, 2011 at 12:23 AM, george hu <geo...@gmail.com> wrote:
> Nope, this will produce the following po text:
> msgid ""
> "Welcome to ${app}, an application generated by${break} the Pyramid web "
> "application development framework. "
> msgstr ""
> and it doesn't work either.

Weird, for me it generates:

msgid ""
"Welcome to ${app}, an application generated by${break} the Pyramid web "
"application development framework."
msgstr ""

Note the ending of this message id "...framework." and yours have
"...framework. " (an extra space).

Douglas

george hu

unread,
Jun 1, 2011, 4:11:58 AM6/1/11
to pylons-...@googlegroups.com
Thank you Douglas! It fixed my problem! Under vim I joined the </p> line with the line above and then deleted the space and then the extra space disappeared in the generated po file. 
But it is strange when I double check my template file, there is no space in the end of line, and when I press return and join the lines again, the space reappears. Maybe there is a problem with my vim settings.
Although the issue is fixed, but it reminds me the following points:
1. Is there a better way to deal with this issue? Since it seems too tricky to spot the problem, either it should be documented or there must be a fix in the chameleon zpt.
2. For the <br i18n:name="break"/> part, it is missed from the documentation of chameleon and pyramid. Finally, I found "Internationalization (i18n) For Developers" in the Plone documentation. It has more detail information about the i18n of zpt. Although it doesn't mention the same issue as we encountered here, it is far more completed and practical. Maybe we can improve the i18n chapter of pyramid, otherwise people like me with none or little zpt experience will be scratching head for hours.


 


Douglas

Malthe Borch

unread,
Jun 1, 2011, 7:30:20 AM6/1/11
to pylons-...@googlegroups.com
On 1 June 2011 10:11, george hu <geo...@gmail.com> wrote:
> Although the issue is fixed, but it reminds me the following points:
> 1. Is there a better way to deal with this issue? Since it seems too tricky
> to spot the problem, either it should be documented or there must be a fix
> in the chameleon zpt.

Which version of Chameleon is this?

> 2. For the <br i18n:name="break"/> part, it is missed from the documentation
> of chameleon and pyramid. Finally, I found "Internationalization (i18n) For
> Developers" in the Plone documentation. It has more detail information about
> the i18n of zpt. Although it doesn't mention the same issue as we
> encountered here, it is far more completed and practical. Maybe we can
> improve the i18n chapter of pyramid, otherwise people like me with none or
> little zpt experience will be scratching head for hours.

Try this:

http://pagetemplates.org/docs/latest/reference.html#i18n-name

Meanwhile, I've considered whether expressions like ${...} should
automatically become i18n:name blocks, e.g. ${1}, ${2} and so forth
for each unnamed apperance (that is, if there's an enclosing
i18n:translate statement).

\malthe

Douglas Cerna

unread,
Jun 1, 2011, 11:43:28 AM6/1/11
to pylons-...@googlegroups.com
On Wed, Jun 1, 2011 at 5:30 AM, Malthe Borch <mbo...@gmail.com> wrote:

> Which version of Chameleon is this?

I've seen it in 1.2.3 and 1.3.0-rc1. I haven't been able to start
pyramid with 2.0.

> Try this:
>
> http://pagetemplates.org/docs/latest/reference.html#i18n-name

Using this template:

----- mytemplate.pt -----
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

xmlns:i18n="http://xml.zope.org/namespaces/i18n" i18n:domain="myapp">
<head>
<title>Testing i18n</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<span i18n:translate="">
<span tal:replace="name" i18n:name="name" /> was born in
<span tal:replace="country_of_birth" i18n:name="country" />.
</span>
</body>
</html>

For this view:

----- views.py -----
@view_config(context=MyModel, renderer='myapp:templates/mytemplate.pt')
def my_view(request):
return {'project':'myapp',
'name':'Foo Bar',
'country_of_birth':'Baz'}

The extracted message is:

----- locale/myapp.pot -----
#: myapp/templates/mytemplate.pt:8
msgid "${name} was born in ${country}. "
msgstr ""

Again, note the white space after the dot. If I initialize a language
and translate this string, after starting the server I see the
original message with the appropriate values from the view:

Foo Bar was born in Baz.

If I change the template to this:

...
<span tal:replace="country_of_birth" i18n:name="country" />.</span>
...

The extracted message is:

#: myapp/templates/mytemplate.pt:8
msgid "${name} was born in ${country}."
msgstr ""

No extra space. And the translation works.

Douglas

george hu

unread,
Jun 1, 2011, 11:44:36 AM6/1/11
to pylons-...@googlegroups.com
Thanks Malthe for pointing out the document. I'm using the version 1.3rc1 of Chameleon as the Babel throws out error when run extract_messages.


--

george hu

unread,
Jun 1, 2011, 12:10:31 PM6/1/11
to pylons-...@googlegroups.com
BTW, as Douglas points out in his reply "Note the use of i18n:name for all the inline markup (including break
tags) and no newline at the end of the element with i18n:translate. " I couldn't find a exact place in the Chameleon document for it.
Reply all
Reply to author
Forward
0 new messages