Take the 2-minute tour ×

I'm using repoze.bfg v1.3 and chameleon v2 (zpt templates). I got troubles with encoding while rendering template:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 9: ordinal not in range(128)

How can i configure repoze.bfg to use utf-8 encoding with chameleon?
I added the following to Configurator:


And hasn't helped.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

The problem is in translationstring library which is used by chameleon. While chameleon can be configured to use different encoding, it pass the data which it get directly to translationstring.
Translationstring in the constructor, where it tries to make unicode from the data it get. The error comes when the data is a non-ascii byte sequence (str in python 2.x).

The solution is to always pass unicode to translation string or update library itself using following diff:

<        self = unicode.__new__(self, msgid)
--- patch
>       try:
>           self = unicode.__new__(self, msgid, "utf8")  # FIXED~
>       except Exception as e:
>           self = unicode.__new__(self,msgid)
share|improve this answer

0xc5 is Å using latin-1, python cannot convert it in utf-8 without knowing the source encoding

if this string comes from a cgi form, make sure the server sets the correct encoding


lynx -dump -mime_header http://url_of_the_page_with_the_form_to_compile|less

and look for something like


if the charset is not utf-8 your configuration is wrong, maybe apache overrides your setting?

share|improve this answer
It's not the case of of server and html. It's template itself. The error raises when template engine try to read template file and insert some dynamic variables. The same can occur without html/http –  Robert Zaremba May 25 '11 at 23:14
so looks like repoze bfg is not able to automatically detect and convert template charset, I'm not sure there is a way to make it read latin1 and convert utf8, but you can always use iconv to convert your templates, some examples here kriyayoga.com/love_blog/post.php/224 –  sherpya May 26 '11 at 10:01
i solved it in different way: play with source code and forced to use utf8 ;) –  Robert Zaremba May 26 '11 at 16:08

Your Answer


