universal data representation/encoding

9 views
Skip to first unread message

blackthorne

unread,
Jun 12, 2009, 7:27:51 PM6/12/09
to web2py Web Framework
I'm trying to display text in a page using an universal encoding such
as UTF-8.
Here is an example:
return dict(message=u'Castro de Avelãs')

in the view I have:

<html><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
...
{{=message}}
...

It doesn't work... I don't get the 'ã' or I get it messed up if I do
message='Castro de Avelãs'

I know for a fact that the problem is not related to the browser and
it's not on the view side because it works fine if I hardcode it (as
in put that string in the html template).

Thank you

Álvaro Justen [Turicas]

unread,
Jun 12, 2009, 10:15:58 PM6/12/09
to web...@googlegroups.com

I had this problem with some browsers (they have a default value to
character encoding and force it instead of value you pass in HTML).
Try to change content-type header inserting charset:

response.headers['Content-Type'] = "text/html;charset=utf-8"

--
Álvaro Justen
Peta5 - Telecomunicações e Software Livre
21 3021-6001 / 9898-0141
http://www.peta5.com.br/

Francisco Gama

unread,
Jun 12, 2009, 10:24:23 PM6/12/09
to web2py Web Framework
I did tried that with but without any success.
as I said, it can't be problem of the browser or the view side since
It does work fine if I write it on the template directly.

On Jun 13, 3:15 am, Álvaro Justen [Turicas] <alvarojus...@gmail.com>
wrote:

Iceberg

unread,
Jun 12, 2009, 10:26:31 PM6/12/09
to web2py Web Framework
No need to u'Castro de Avelãs', just 'Castro de Avelãs', providing
that your source code itself is in utf8 (by default, it is).

Francisco Gama

unread,
Jun 12, 2009, 10:30:57 PM6/12/09
to web2py Web Framework
as you can see on my first post in this thread I also tried it that
way but I get it messed up with
message='Castro de Avelãs'. Something like, 'Castro de Avel?s'

with message=u'Castro de Avelãs' I get 'Castro de Avels'

Iceberg

unread,
Jun 12, 2009, 10:38:01 PM6/12/09
to web2py Web Framework
Mmm, try putting these lines at the beginning of your controller and
see whether it makes difference?

#!/usr/bin/env python
# coding: utf8

Francisco Gama

unread,
Jun 12, 2009, 10:42:48 PM6/12/09
to web2py Web Framework
Thank you!
That did the trick :)

Francisco Gama

unread,
Jun 12, 2009, 10:44:19 PM6/12/09
to web2py Web Framework
Massimo, I would suggest this correction by Iceberg as default for
controller and model templates...

On Jun 13, 3:38 am, Iceberg <iceb...@21cn.com> wrote:

Iceberg

unread,
Jun 12, 2009, 10:52:14 PM6/12/09
to web2py Web Framework
Me too. :-) After all, I've been done that manually for years. Should
propose that much earlier.

mdipierro

unread,
Jun 12, 2009, 11:06:05 PM6/12/09
to web2py Web Framework
Given that they are executed, not imported the first of these two
lines

#!/usr/bin/env python
# coding: utf8

seems inappropriate. Will the second line be sufficient?

Francisco Gama

unread,
Jun 12, 2009, 11:09:25 PM6/12/09
to web2py Web Framework
yes, I assumed that one.

As to your question, I just tried just with the second line (#coding:
utf8) and yes, it worked fine.

:)

mdipierro

unread,
Jun 12, 2009, 11:45:28 PM6/12/09
to web2py Web Framework
ok, I changed the scaffolding add and admin accordingly. Uploading to
trunk now.

Massimo

Francisco Gama

unread,
Jun 17, 2009, 12:16:31 AM6/17/09
to web2py Web Framework
ok, this one is solved but there is a situation where things still put
me away.
it's when I use T() in views with strings that contain special
characters like ç, á, à...

e.g.: {{=T('A pé em Bragança')}}
This would fail the same way on the special characters.

Thank you, best regards

On Jun 13, 4:06 am, mdipierro <mdipie...@cs.depaul.edu> wrote:

mdipierro

unread,
Jun 17, 2009, 2:18:23 AM6/17/09
to web2py Web Framework
because you should use UTF8 in T.

Iceberg

unread,
Jun 17, 2009, 2:18:32 AM6/17/09
to web2py Web Framework
On Jun17, 12:16pm, Francisco Gama <Francisco....@gmail.com> wrote:
> ok, this one is solved but there is a situation where things still put
> me away.
> it's when I use T() in views with strings that contain special
> characters like ç, á, à...
>
> e.g.: {{=T('A pé em Bragança')}}
> This would fail the same way on the special characters.
>
> Thank you, best regards

I did not try that, but I assume that is not the way it supposed to
be. As a tradition, most people should do T('An English Message, in
ASCII of course'), then translate it into other language. But not the
reverse.

Francisco Gama

unread,
Jun 17, 2009, 7:38:38 AM6/17/09
to web2py Web Framework
That is not a solution. Personal Names have no language as well as
city names like the one in the example: "Bragança".
So T() should allow UTF-8.

Massimo, how?

Iceberg

unread,
Jun 17, 2009, 9:55:38 PM6/17/09
to web2py Web Framework
If so, why do you need T('UTF8-string') after all? You can just simply
use "raw" utf8 string, such as:
{{= "Bragança"}}
or even:
<body> Bragança </body>

In other words, if you don't need i18n, you don't need to bother T
('...'). Fair enough?

AchipA

unread,
Jun 18, 2009, 5:31:05 AM6/18/09
to web2py Web Framework
I think we're forgetting an important lesson from gettext here...

What you include in T() is NOT the message. It's an ASCII key. Of
course, most people make the key identical to the English message
string for convenience as it's the fallback, too.

Francisco Gama

unread,
Jun 18, 2009, 6:19:50 AM6/18/09
to web2py Web Framework
you are right.

It's just more "rapid" to write the things we want in a language that
we want, and then just translate.

I will do that way
Reply all
Reply to author
Forward
0 new messages