i18n support in template file?

45 views
Skip to first unread message

Bibby

unread,
Nov 29, 2008, 8:59:10 AM11/29/08
to web.py
Hi, all.

I use gettext for i18n support, and it works. But I also need i18n
support in template file, like this (_("Message") in code below):

----<----
$def with (name)

<html>
<head><title>Test</title></head>
<body>
_("Message"): ${name}
</body>
</html>
----<----

But webpy output '_("Message")' directly, how can i make it output the
translated string?

Aaron Swartz

unread,
Nov 29, 2008, 10:19:56 AM11/29/08
to we...@googlegroups.com
$_('Message')

Zhang Huangbin

unread,
Nov 29, 2008, 10:31:42 AM11/29/08
to web.py


On Nov 29, 11:19 pm, "Aaron Swartz" <m...@aaronsw.com> wrote:
> $_('Message')

I added this line in my script, and use $_('Message') in template
file, it works:

web.template.Template.globals['_'] = gettext.gettext

Thanks Aaron. :)

Zhang Huangbin

unread,
Nov 29, 2008, 10:58:38 AM11/29/08
to web.py


On Nov 29, 11:19 pm, "Aaron Swartz" <m...@aaronsw.com> wrote:
> $_('Message')

One more question, is there any method to extract these string from
template file? like pygettext.py in Python-2.6, or babel?

Thanks :)

Zhang Huangbin

unread,
Nov 29, 2008, 11:00:50 AM11/29/08
to web.py
Oops, pygettext.py can extract it (I tested before, but directory path
was wrong. :().

Sorry for my mistake. Thanks again.

Zhang Huangbin

unread,
Nov 29, 2008, 11:23:05 AM11/29/08
to web.py


On Nov 29, 11:19 pm, "Aaron Swartz" <m...@aaronsw.com> wrote:
> $_('Message')

Oops, it doesn't work.

'Message' is translated to 'This is Message', but $_('Message') just
output as 'Message', not 'This is Message'.

What's wrong with my code?

Thanks very much.

andrei

unread,
Nov 29, 2008, 2:07:41 PM11/29/08
to web.py
I use this way too, to extract messages from templates i use python
babel javascript extraction method, with keyword '$_'
I know it is bad, but it worked for most of the strings.

I wish there was extraction method for webpy templates, as it is for
other templates http://babel.edgewall.org/wiki/BabelPlugins
It would be awesome if somebody has written this. I'm just not good in
writing parser stuff

andrei

unread,
Nov 29, 2008, 2:13:05 PM11/29/08
to web.py
Oh, please tell me how do you use it with pygettext.py

Zhang Huangbin

unread,
Nov 29, 2008, 8:23:57 PM11/29/08
to web.py


On Nov 30, 3:13 am, andrei <Andre...@gmail.com> wrote:
> Oh, please tell me how do you use it with pygettext.py

Hi, andrei.

Here is my code:
----
python pygettext.py -a -v \
-d ${DOMAIN} \
-o ${DOMAIN}.po \
../*.py ../libs/*.py ../templates/default/*.html
----

It will print msg like this:
----
Working on ../code.py
Working on ../templates/default/index.html
----

The content of pot file:
----
#
# ... skip some lines here ...
#

#: ../code.py:40
msgid "Message"
msgstr ""

#: ../templates/default/index.html:6
msgid "World"
msgstr ""
----

But i still can't make the $_("Message") work in template file (not as
variable).

andrei

unread,
Nov 30, 2008, 4:12:48 AM11/30/08
to web.py
Thanks, i will try this way. Does it work for javascript files as
well?

> web.template.Template.globals['_'] = gettext.gettext
if you are using webpy 0.3, then this is deprecated

I'm using something like this

lang = gettext.translation('messages', os.path.join(os.path.dirname
(__file__), config['locale_dir']) , languages=config['languages'])
lang.install(unicode=1)
render = web.template.render('templates/', globals={'_': _})

andrei

unread,
Nov 30, 2008, 7:24:40 AM11/30/08
to web.py
So I've tried pygettext.py, but it didn't work, it throws out an
error:
"IndentationError: unindent does not match any outer indentation
level"

I guess, its because pygettext is designed to extract messages from
python files

Zhang Huangbin

unread,
Nov 30, 2008, 9:41:46 AM11/30/08
to web.py

Hi, andrei.

I use your code, and now i have i18n support in webpy template file.
Thanks very much. :)

On Nov 30, 8:24 pm, andrei <Andre...@gmail.com> wrote:
> I guess, its because pygettext is designed to extract messages from
> python files

I use pygettext.py to extract messages from webpy template files, and
it works. I don't know why you got this error msg. :(

Zhang Huangbin

unread,
Nov 30, 2008, 10:21:41 AM11/30/08
to web.py
I submitted this tip in cookbook:
http://webpy.org/cookbook/i18n_support_in_template_file

Please help to review it. Thanks very much.

andrei

unread,
Nov 30, 2008, 1:02:37 PM11/30/08
to web.py
On Nov 30, 5:41 pm, Zhang Huangbin <michaelbi...@gmail.com> wrote:
> I use pygettext.py to extract messages from webpy template files, and
> it works. I don't know why you got this error msg. :(

Can you please try to extract message from this file: http://old.etoleto.com/base.html
and tell me if it works ?
I'm getting this:

Traceback (most recent call last):
File "pygettext.py", line 664, in <module>
main()
File "pygettext.py", line 639, in main
tokenize.tokenize(fp.readline, eater)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/tokenize.py", line 153, in tokenize
tokenize_loop(readline, tokeneater)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/tokenize.py", line 159, in tokenize_loop
for token_info in generate_tokens(readline):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/tokenize.py", line 277, in generate_tokens
("<tokenize>", lnum, pos, line))
File "<tokenize>", line 12
<link rel="stylesheet" href="/static/style.css" type="text/css"
media="screen" charset="utf-8"/>
^

Zhang Huangbin

unread,
Nov 30, 2008, 8:41:13 PM11/30/08
to web.py


andrei wrote:
> On Nov 30, 5:41�pm, Zhang Huangbin <michaelbi...@gmail.com> wrote:
> > I use pygettext.py to extract messages from webpy template files, and
> > it works. I don't know why you got this error msg. :(
>
> Can you please try to extract message from this file: http://old.etoleto.com/base.html
> and tell me if it works ?

Hi, Andrei.

file: templates/default/base.html
----<----
$def with (page=None, title=None) $if title: $else: $:js
(session.section) $:css(session.section)
FleXoft

$if session.user: ${session.user.login}: $for url in urls
(session.user.type): $if not loop.first: / $url.name / $_('Logout')
$:page
----<----

Shell command:
----<----
$ python pygettext.py -a -v -d messages -o messages.po ../*.py ../
templates/default/*.html
----<----

In messages.po ('Logout' is only used in base.html, so it's extracted
from it):
----<----
#: ../templates/default/base.html:4
msgid "Logout"
msgstr ""
----<----

andrei

unread,
Dec 1, 2008, 4:32:52 PM12/1/08
to web.py
Well, it doesn't work for me.
I've got a new idea. Since webpy translates templates to python code
before compiling them - then it would be possible to somehow extract
messages from the python code of template.
I will try to dig this way.

andrei

unread,
Dec 1, 2008, 5:42:54 PM12/1/08
to web.py
I came with this extraction method for babel:

from web.template import Template
import StringIO
from babel.messages.extract import extract_python

def extract_webpy(fileobj, keywords, comment_tags, options):
code = Template.generate_code(fileobj.read(), fileobj.name)
return extract_python(StringIO.StringIO(code), keywords,
comment_tags, options)

haven't tested it yet, but i think it should work :)

andrei

unread,
Dec 2, 2008, 4:05:47 AM12/2/08
to web.py
yes, it works
Reply all
Reply to author
Forward
0 new messages